oliverdavies.uk/content/node.95f13efa-f3ff-4a73-a562-9cd44264ebe9.json
2025-05-11 20:02:13 +01:00

100 lines
No EOL
7 KiB
JSON

{
"uuid": [
{
"value": "95f13efa-f3ff-4a73-a562-9cd44264ebe9"
}
],
"langcode": [
{
"value": "en"
}
],
"type": [
{
"target_id": "daily_email",
"target_type": "node_type",
"target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7"
}
],
"revision_timestamp": [
{
"value": "2025-04-21T01:21:42+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-21T01:21:42+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\">&lt;?php\n\nclass MyClass {\n\n public function __construct(\n private DependencyInterface $dependency\n ) {\n }\n\n public function doSomething() {\n $result = $this-&gt;dependency-&gt;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\">&lt;?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\">&lt;?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-&gt;createMock(DependencyInterface::class);\n\n \/\/ Setting up the mock to return a specific value.\n $dependencyMock-&gt;expects($this-&gt;once())\n -&gt;method('performAction')\n -&gt;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-&gt;doSomething();\n\n \/\/ Asserting that the result matches the expected value.\n $this-&gt;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\">&lt;?php\n\nclass MyClass {\n\n public function __construct(\n private DependencyInterface $dependency\n ) {\n }\n\n public function doSomething() {\n $result = $this-&gt;dependency-&gt;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\">&lt;?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\">&lt;?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-&gt;createMock(DependencyInterface::class);\n\n \/\/ Setting up the mock to return a specific value.\n $dependencyMock-&gt;expects($this-&gt;once())\n -&gt;method('performAction')\n -&gt;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-&gt;doSomething();\n\n \/\/ Asserting that the result matches the expected value.\n $this-&gt;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-21T01:21:42+00:00",
"guid": null,
"hash": "db65f35f5c7767e34b717942905448f4",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}