{ "uuid": [ { "value": "1fd87ecf-4269-4874-859e-d49c238694bf" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:28+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-05-11T09:00:28+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

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

Here's an example (thanks, ChatGPT, for the code).<\/p>\n\n

The Class to be tested (MyClass.php)<\/h2>\n\n
<?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

Dependency Interface (DependencyInterface.php)<\/h2>\n\n
<?php\n\ninterface DependencyInterface {\n\n  public function performAction();\n\n}\n<\/code><\/pre>\n\n

A test class that ends up testing mocks (MyClassTest.php)<\/h2>\n\n
<?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

Here's the thing<\/h2>\n\n

In this example, the test creates a mock for the DependencyInterface<\/code> and sets up an expectation that the performAction method will be called once, returning a specific value.<\/p>\n\n

The test then calls the doSomething<\/code> method on MyClass<\/code> and asserts that the result is as expected.<\/p>\n\n

The issue with this test is that it's not testing the actual behaviour of MyClass<\/code>.<\/p>\n\n

It's only testing that the mock is configured correctly.<\/p>\n\n

If the real implementation of MyClass<\/code> has a bug, this test won't catch it.<\/p>\n\n ", "format": "full_html", "processed": "\n

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

Here's an example (thanks, ChatGPT, for the code).<\/p>\n\n

The Class to be tested (MyClass.php)<\/h2>\n\n
<?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

Dependency Interface (DependencyInterface.php)<\/h2>\n\n
<?php\n\ninterface DependencyInterface {\n\n  public function performAction();\n\n}\n<\/code><\/pre>\n\n

A test class that ends up testing mocks (MyClassTest.php)<\/h2>\n\n
<?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

Here's the thing<\/h2>\n\n

In this example, the test creates a mock for the DependencyInterface<\/code> and sets up an expectation that the performAction method will be called once, returning a specific value.<\/p>\n\n

The test then calls the doSomething<\/code> method on MyClass<\/code> and asserts that the result is as expected.<\/p>\n\n

The issue with this test is that it's not testing the actual behaviour of MyClass<\/code>.<\/p>\n\n

It's only testing that the mock is configured correctly.<\/p>\n\n

If the real implementation of MyClass<\/code> has a bug, this test won't catch it.<\/p>\n\n ", "summary": null } ] }