oliverdavies.uk/content/node.001f3ebb-79dc-43ad-a363-6dc668d06a0c.yml

133 lines
4.8 KiB
YAML

uuid:
- value: 001f3ebb-79dc-43ad-a363-6dc668d06a0c
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:44+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: |
Structure a new test by writing comments first
created:
- value: '2023-04-26T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:44+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2023/04/26/tdd-structure-a-new-test-by-writing-comments-first
langcode: en
body:
- value: |
<p>Test cases are usually split into two or three sections - "Arrange, Act, Assert" or "Given, When, Then".</p>
<p>If the test has a prerequisite, such as some users or content to be created or in a given state, you create the required environment for the test. With unit tests, this would include mocking any dependencies you need to use.</p>
<p>Then you perform an action on the subject under test and, finally, assert that the system is in the desired state.</p>
<p>Maybe a user was pending initially, and they're active after running a command or a cron task. You can assert the initial state of the user as well as the final state to ensure the action did what it was supposed to do.</p>
<h2 id="option-1">Option 1</h2>
<p>To help me get started, I'll sometimes write a test like this with placeholders to separate the test into its separate stages:</p>
<pre><code class="language-php">/** @test */
function should_activate_a_pending_user(): void {
// Arrange.
// Act.
// Assert.
}
</code></pre>
<p>This makes me think about the different stages and what each might need to contain.</p>
<h2 id="option-2">Option 2</h2>
<p>Or I might write it out in the "Given, When, Then" format:</p>
<pre><code class="language-php">/** @test */
function should_activate_a_pending_user(): void {
// Given I have a user.
// And the user is pending.
// When I run the user update command.
// Then the user should no longer be 'pending'.
// And the user status should be 'active'.
}
</code></pre>
<p>This takes a little longer to write but feels more familiar if you're used to a behavioural testing framework like Behat.</p>
<p>Either way, sometimes, I'll remove the comments once I've written the code around them or leave them to provide additional context.</p>
format: full_html
processed: |
<p>Test cases are usually split into two or three sections - "Arrange, Act, Assert" or "Given, When, Then".</p>
<p>If the test has a prerequisite, such as some users or content to be created or in a given state, you create the required environment for the test. With unit tests, this would include mocking any dependencies you need to use.</p>
<p>Then you perform an action on the subject under test and, finally, assert that the system is in the desired state.</p>
<p>Maybe a user was pending initially, and they're active after running a command or a cron task. You can assert the initial state of the user as well as the final state to ensure the action did what it was supposed to do.</p>
<h2 id="option-1">Option 1</h2>
<p>To help me get started, I'll sometimes write a test like this with placeholders to separate the test into its separate stages:</p>
<pre><code class="language-php">/** @test */
function should_activate_a_pending_user(): void {
// Arrange.
// Act.
// Assert.
}
</code></pre>
<p>This makes me think about the different stages and what each might need to contain.</p>
<h2 id="option-2">Option 2</h2>
<p>Or I might write it out in the "Given, When, Then" format:</p>
<pre><code class="language-php">/** @test */
function should_activate_a_pending_user(): void {
// Given I have a user.
// And the user is pending.
// When I run the user update command.
// Then the user should no longer be 'pending'.
// And the user status should be 'active'.
}
</code></pre>
<p>This takes a little longer to write but feels more familiar if you're used to a behavioural testing framework like Behat.</p>
<p>Either way, sometimes, I'll remove the comments once I've written the code around them or leave them to provide additional context.</p>
summary: null
field_daily_email_cta: { }