100 lines
No EOL
7 KiB
JSON
100 lines
No EOL
7 KiB
JSON
{
|
|
"uuid": [
|
|
{
|
|
"value": "077780fd-8480-426c-80b9-d54befef06b1"
|
|
}
|
|
],
|
|
"langcode": [
|
|
{
|
|
"value": "en"
|
|
}
|
|
],
|
|
"type": [
|
|
{
|
|
"target_id": "daily_email",
|
|
"target_type": "node_type",
|
|
"target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7"
|
|
}
|
|
],
|
|
"revision_timestamp": [
|
|
{
|
|
"value": "2025-04-16T14:13:01+00:00"
|
|
}
|
|
],
|
|
"revision_uid": [
|
|
{
|
|
"target_type": "user",
|
|
"target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849"
|
|
}
|
|
],
|
|
"revision_log": [],
|
|
"status": [
|
|
{
|
|
"value": true
|
|
}
|
|
],
|
|
"uid": [
|
|
{
|
|
"target_type": "user",
|
|
"target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849"
|
|
}
|
|
],
|
|
"title": [
|
|
{
|
|
"value": "Avoiding over-mocking\n"
|
|
}
|
|
],
|
|
"created": [
|
|
{
|
|
"value": "2023-11-16T00:00:00+00:00"
|
|
}
|
|
],
|
|
"changed": [
|
|
{
|
|
"value": "2025-04-16T14:13:01+00:00"
|
|
}
|
|
],
|
|
"promote": [
|
|
{
|
|
"value": false
|
|
}
|
|
],
|
|
"sticky": [
|
|
{
|
|
"value": false
|
|
}
|
|
],
|
|
"default_langcode": [
|
|
{
|
|
"value": true
|
|
}
|
|
],
|
|
"revision_translation_affected": [
|
|
{
|
|
"value": true
|
|
}
|
|
],
|
|
"path": [
|
|
{
|
|
"alias": "\/daily\/2023\/11\/16\/avoiding-over-mocking",
|
|
"langcode": "en"
|
|
}
|
|
],
|
|
"body": [
|
|
{
|
|
"value": "\n <p>In unit tests, and sometimes in kernel tests, you need to mock the dependencies you aren't testing, but you can over-mock and only be testing the mocks and not the code you want to test.<\/p>\n\n<p>Here's an example (thanks, ChatGPT, for the code).<\/p>\n\n<h2 id=\"the-class-to-be-tested-myclass.php\">The Class to be tested (MyClass.php)<\/h2>\n\n<pre><code class=\"language-php\"><?php\n\nclass MyClass {\n\n public function __construct(\n private DependencyInterface $dependency\n ) {\n }\n\n public function doSomething() {\n $result = $this->dependency->performAction();\n\n return \"Result: \" . $result;\n }\n}\n<\/code><\/pre>\n\n<h2 id=\"dependency-interface-dependencyinterface.php\">Dependency Interface (DependencyInterface.php)<\/h2>\n\n<pre><code class=\"language-php\"><?php\n\ninterface DependencyInterface {\n\n public function performAction();\n\n}\n<\/code><\/pre>\n\n<h2 id=\"a-test-class-that-ends-up-testing-mocks-myclasstest.php\">A test class that ends up testing mocks (MyClassTest.php)<\/h2>\n\n<pre><code class=\"language-php\"><?php\n\nuse PHPUnit\\Framework\\TestCase;\n\nclass MyClassTest extends TestCase {\n\n public function testDoSomething() {\n \/\/ Creating a mock of the DependencyInterface.\n $dependencyMock = $this->createMock(DependencyInterface::class);\n\n \/\/ Setting up the mock to return a specific value.\n $dependencyMock->expects($this->once())\n ->method('performAction')\n ->willReturn('Mocked result');\n\n \/\/ Creating an instance of MyClass with the mock.\n $myClass = new MyClass($dependencyMock);\n\n \/\/ Calling the method to be tested.\n $result = $myClass->doSomething();\n\n \/\/ Asserting that the result matches the expected value.\n $this->assertEquals('Result: Mocked result', $result);\n }\n\n}\n<\/code><\/pre>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>In this example, the test creates a mock for the <code>DependencyInterface<\/code> and sets up an expectation that the performAction method will be called once, returning a specific value.<\/p>\n\n<p>The test then calls the <code>doSomething<\/code> method on <code>MyClass<\/code> and asserts that the result is as expected.<\/p>\n\n<p>The issue with this test is that it's not testing the actual behaviour of <code>MyClass<\/code>.<\/p>\n\n<p>It's only testing that the mock is configured correctly.<\/p>\n\n<p>If the real implementation of <code>MyClass<\/code> has a bug, this test won't catch it.<\/p>\n\n ",
|
|
"format": "full_html",
|
|
"processed": "\n <p>In unit tests, and sometimes in kernel tests, you need to mock the dependencies you aren't testing, but you can over-mock and only be testing the mocks and not the code you want to test.<\/p>\n\n<p>Here's an example (thanks, ChatGPT, for the code).<\/p>\n\n<h2 id=\"the-class-to-be-tested-myclass.php\">The Class to be tested (MyClass.php)<\/h2>\n\n<pre><code class=\"language-php\"><?php\n\nclass MyClass {\n\n public function __construct(\n private DependencyInterface $dependency\n ) {\n }\n\n public function doSomething() {\n $result = $this->dependency->performAction();\n\n return \"Result: \" . $result;\n }\n}\n<\/code><\/pre>\n\n<h2 id=\"dependency-interface-dependencyinterface.php\">Dependency Interface (DependencyInterface.php)<\/h2>\n\n<pre><code class=\"language-php\"><?php\n\ninterface DependencyInterface {\n\n public function performAction();\n\n}\n<\/code><\/pre>\n\n<h2 id=\"a-test-class-that-ends-up-testing-mocks-myclasstest.php\">A test class that ends up testing mocks (MyClassTest.php)<\/h2>\n\n<pre><code class=\"language-php\"><?php\n\nuse PHPUnit\\Framework\\TestCase;\n\nclass MyClassTest extends TestCase {\n\n public function testDoSomething() {\n \/\/ Creating a mock of the DependencyInterface.\n $dependencyMock = $this->createMock(DependencyInterface::class);\n\n \/\/ Setting up the mock to return a specific value.\n $dependencyMock->expects($this->once())\n ->method('performAction')\n ->willReturn('Mocked result');\n\n \/\/ Creating an instance of MyClass with the mock.\n $myClass = new MyClass($dependencyMock);\n\n \/\/ Calling the method to be tested.\n $result = $myClass->doSomething();\n\n \/\/ Asserting that the result matches the expected value.\n $this->assertEquals('Result: Mocked result', $result);\n }\n\n}\n<\/code><\/pre>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>In this example, the test creates a mock for the <code>DependencyInterface<\/code> and sets up an expectation that the performAction method will be called once, returning a specific value.<\/p>\n\n<p>The test then calls the <code>doSomething<\/code> method on <code>MyClass<\/code> and asserts that the result is as expected.<\/p>\n\n<p>The issue with this test is that it's not testing the actual behaviour of <code>MyClass<\/code>.<\/p>\n\n<p>It's only testing that the mock is configured correctly.<\/p>\n\n<p>If the real implementation of <code>MyClass<\/code> has a bug, this test won't catch it.<\/p>\n\n ",
|
|
"summary": null
|
|
}
|
|
],
|
|
"feeds_item": [
|
|
{
|
|
"imported": "2025-04-16T14:13:01+00:00",
|
|
"guid": null,
|
|
"hash": "db65f35f5c7767e34b717942905448f4",
|
|
"target_type": "feeds_feed",
|
|
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
|
|
}
|
|
]
|
|
} |