100 lines
No EOL
6.4 KiB
JSON
100 lines
No EOL
6.4 KiB
JSON
{
|
|
"uuid": [
|
|
{
|
|
"value": "f0598e12-6cd1-46f2-af21-3c57f5b65d83"
|
|
}
|
|
],
|
|
"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": "Partial mocking\n"
|
|
}
|
|
],
|
|
"created": [
|
|
{
|
|
"value": "2023-11-23T00: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\/23\/partial-mocking",
|
|
"langcode": "en"
|
|
}
|
|
],
|
|
"body": [
|
|
{
|
|
"value": "\n <p>Today, I wrote a test whilst fixing a bug in some legacy custom Drupal code.<\/p>\n\n<p>The code is for a custom block, which can be configured using user-defined settings - including a link URL.<\/p>\n\n<p>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<h2 id=\"my-first-attempt\">My first attempt<\/h2>\n\n<p>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<p>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<p>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<h2 id=\"my-second-attempt\">My second attempt<\/h2>\n\n<p>My second attempt used kernel\/integration tests, but as there's no <code>setParameter()<\/code> method on the route matcher I could use in the test, I'd need to rely on mocking.<\/p>\n\n<p>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<h2 id=\"the-result\">The result<\/h2>\n\n<p>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<p>Everything else remained the same.<\/p>\n\n<p>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<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>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<p>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<p>Whilst I'm aware of over-using mocks, this was an ideal situation to use them.<\/p>\n\n ",
|
|
"format": "full_html",
|
|
"processed": "\n <p>Today, I wrote a test whilst fixing a bug in some legacy custom Drupal code.<\/p>\n\n<p>The code is for a custom block, which can be configured using user-defined settings - including a link URL.<\/p>\n\n<p>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<h2 id=\"my-first-attempt\">My first attempt<\/h2>\n\n<p>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<p>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<p>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<h2 id=\"my-second-attempt\">My second attempt<\/h2>\n\n<p>My second attempt used kernel\/integration tests, but as there's no <code>setParameter()<\/code> method on the route matcher I could use in the test, I'd need to rely on mocking.<\/p>\n\n<p>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<h2 id=\"the-result\">The result<\/h2>\n\n<p>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<p>Everything else remained the same.<\/p>\n\n<p>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<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>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<p>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<p>Whilst I'm aware of over-using mocks, this was an ideal situation to use them.<\/p>\n\n ",
|
|
"summary": null
|
|
}
|
|
],
|
|
"feeds_item": [
|
|
{
|
|
"imported": "2025-04-21T01:21:42+00:00",
|
|
"guid": null,
|
|
"hash": "f5ae32e6a60dafd8afa4ab8aa2073638",
|
|
"target_type": "feeds_feed",
|
|
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
|
|
}
|
|
]
|
|
} |