oliverdavies.uk/content/node.fbc1e9f3-0957-4dfb-b273-f3ed4a6c40f7.yml

97 lines
4.4 KiB
YAML

uuid:
- value: fbc1e9f3-0957-4dfb-b273-f3ed4a6c40f7
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:50+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: |
Tests are living documentation
created:
- value: '2022-12-30T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:50+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2022/12/30/tests-are-living-documentation
langcode: en
body:
- value: |
<p>Today I was working on a project and made a one-line change that updated a single value within an API response.</p>
<p>Unexpectedly, it caused a test to fail. The API response returned a 500 status code instead of the expected 201 code.</p>
<p>I reverted the change locally and ensured that the test passed again, so I knew it was causing the failure.</p>
<h2 id="fixing-the-failure">Fixing the failure</h2>
<p>The change was removing a hard-coded part of a URL to a dynamic one, using Drupal's <code>Settings</code> class.</p>
<p>I was retrieving a value from it, but as there was no value being set within the test, it was returning a null value and causing the 500 error code.</p>
<h2 id="how-does-the-settings-class-work%3F">How does the Settings class work?</h2>
<p>To fix the test failure, I needed to learn how to set a Settings value within a test.</p>
<p>To do this, I looked for and found the test for the Settings class itself. I saw how it was being set there, did the same in my test, fixed the failure and got my test passing again.</p>
<h2 id="tests-are-living-documentation">Tests are living documentation</h2>
<p>As well as verifying things work when they are written, tests also act as long-term documentation. They can be run at any point to ensure that they still pass and are a reference to other developers on how the code should work.</p>
<p>Rather than a Confluence page, a README file or code comments which can become out of date, if a test becomes outdated, it will fail and make developers aware, as well as break any CI pipeline that it runs in.</p>
<p>By writing tests, you're ensuring that your code works as expected and documenting it at the same time.</p>
format: full_html
processed: |
<p>Today I was working on a project and made a one-line change that updated a single value within an API response.</p>
<p>Unexpectedly, it caused a test to fail. The API response returned a 500 status code instead of the expected 201 code.</p>
<p>I reverted the change locally and ensured that the test passed again, so I knew it was causing the failure.</p>
<h2 id="fixing-the-failure">Fixing the failure</h2>
<p>The change was removing a hard-coded part of a URL to a dynamic one, using Drupal's <code>Settings</code> class.</p>
<p>I was retrieving a value from it, but as there was no value being set within the test, it was returning a null value and causing the 500 error code.</p>
<h2 id="how-does-the-settings-class-work%3F">How does the Settings class work?</h2>
<p>To fix the test failure, I needed to learn how to set a Settings value within a test.</p>
<p>To do this, I looked for and found the test for the Settings class itself. I saw how it was being set there, did the same in my test, fixed the failure and got my test passing again.</p>
<h2 id="tests-are-living-documentation">Tests are living documentation</h2>
<p>As well as verifying things work when they are written, tests also act as long-term documentation. They can be run at any point to ensure that they still pass and are a reference to other developers on how the code should work.</p>
<p>Rather than a Confluence page, a README file or code comments which can become out of date, if a test becomes outdated, it will fail and make developers aware, as well as break any CI pipeline that it runs in.</p>
<p>By writing tests, you're ensuring that your code works as expected and documenting it at the same time.</p>
summary: null
field_daily_email_cta: { }