{ "uuid": [ { "value": "39db6489-2e23-4cb3-a0be-a4e78a2e9142" } ], "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:26+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": "Partial mocking\n" } ], "created": [ { "value": "2023-11-23T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:26+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2023\/11\/23\/partial-mocking", "langcode": "en" } ], "body": [ { "value": "\n
Today, I wrote a test whilst fixing a bug in some legacy custom Drupal code.<\/p>\n\n
The code is for a custom block, which can be configured using user-defined settings - including a link URL.<\/p>\n\n
In this case, if the link was null (one hadn't been provided), the link was generated to the home page instead of the desired destination.<\/p>\n\n
There was a combination of settings needed to replicate the bug, such as the current site language, the node type the block was placed on and, of course, an empty link URL.<\/p>\n\n
Because the block uses the current route to get the current node, my first attempt to test this was to use a browser\/functional test.<\/p>\n\n
That failed quickly after having to enable various other custom modules due to dependencies and to add and configure unrelated configuration settings.<\/p>\n\n
My second attempt used kernel\/integration tests, but as there's no In a unit test, everything needs to be mocked, but a kernel test allows me to be more selective, only mock what I need, and use the real services for everything else - a.k.a. partial mocking.<\/p>\n\n I replicated the bug by setting the default and current languages, creating a mock language manager, creating a node of the required type and returning it from a mocked route match.<\/p>\n\n Everything else remained the same.<\/p>\n\n Then, I fixed the bug and used a data provider to provide different variables into the test so each use case was covered.<\/p>\n\n The whole test file is 129 lines and would have been much more if I'd had to replicate all the configuration in a functional test or mock everything in a unit test.<\/p>\n\n An integration test with partial mocking was ideal in this case, as it gave me the most flexibility to test what I needed whilst keeping the code simple.<\/p>\n\n Whilst I'm aware of over-using mocks, this was an ideal situation to use them.<\/p>\n\n ",
"format": "full_html",
"processed": "\n Today, I wrote a test whilst fixing a bug in some legacy custom Drupal code.<\/p>\n\n The code is for a custom block, which can be configured using user-defined settings - including a link URL.<\/p>\n\n In this case, if the link was null (one hadn't been provided), the link was generated to the home page instead of the desired destination.<\/p>\n\n There was a combination of settings needed to replicate the bug, such as the current site language, the node type the block was placed on and, of course, an empty link URL.<\/p>\n\n Because the block uses the current route to get the current node, my first attempt to test this was to use a browser\/functional test.<\/p>\n\n That failed quickly after having to enable various other custom modules due to dependencies and to add and configure unrelated configuration settings.<\/p>\n\n My second attempt used kernel\/integration tests, but as there's no In a unit test, everything needs to be mocked, but a kernel test allows me to be more selective, only mock what I need, and use the real services for everything else - a.k.a. partial mocking.<\/p>\n\n I replicated the bug by setting the default and current languages, creating a mock language manager, creating a node of the required type and returning it from a mocked route match.<\/p>\n\n Everything else remained the same.<\/p>\n\n Then, I fixed the bug and used a data provider to provide different variables into the test so each use case was covered.<\/p>\n\n The whole test file is 129 lines and would have been much more if I'd had to replicate all the configuration in a functional test or mock everything in a unit test.<\/p>\n\n An integration test with partial mocking was ideal in this case, as it gave me the most flexibility to test what I needed whilst keeping the code simple.<\/p>\n\n Whilst I'm aware of over-using mocks, this was an ideal situation to use them.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "1970-01-01T00:33:45+00:00",
"guid": null,
"hash": "f5ae32e6a60dafd8afa4ab8aa2073638",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}setParameter()<\/code> method on the route matcher I could use in the test, I'd need to rely on mocking.<\/p>\n\n
The result<\/h2>\n\n
Here's the thing<\/h2>\n\n
My first attempt<\/h2>\n\n
My second attempt<\/h2>\n\n
setParameter()<\/code> method on the route matcher I could use in the test, I'd need to rely on mocking.<\/p>\n\n
The result<\/h2>\n\n
Here's the thing<\/h2>\n\n