oliverdavies.uk/content/node.b782394b-8024-437f-bc09-9755abe36345.json
2025-05-11 20:02:13 +01:00

100 lines
No EOL
6.1 KiB
JSON

{
"uuid": [
{
"value": "b782394b-8024-437f-bc09-9755abe36345"
}
],
"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:31+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": "Writing assertions first"
}
],
"created": [
{
"value": "2024-06-05T00:00:00+00:00"
}
],
"changed": [
{
"value": "2025-04-21T01:21:31+00:00"
}
],
"promote": [
{
"value": false
}
],
"sticky": [
{
"value": false
}
],
"default_langcode": [
{
"value": true
}
],
"revision_translation_affected": [
{
"value": true
}
],
"path": [
{
"alias": "\/daily\/2024\/06\/05\/writing-assertions-first",
"langcode": null
}
],
"body": [
{
"value": "\n <p>As well as <a href=\"https:\/\/www.oliverdavies.uk\/daily\/2024\/06\/03\/writing-comments-first\">writing comments first<\/a>, when writing tests, I sometimes like to write my tests backwards and start by writing the assertions first.<\/p>\n\n<p>I know what I want to assert in the test, so it's an easy place to start.<\/p>\n\n<p>I'll run it, see the error, fix it and continue working backwards.<\/p>\n\n<p>For example, I could start with this:<\/p>\n\n<pre><code class=\"php\">public function testOnlyPostNodesAreShown(): void {\n $assert = $this-&gt;assertSession();\n $assert-&gt;pageTextContains('Post one');\n $assert-&gt;pageTextContains('Post two');\n $assert-&gt;pageTextNotContains('This is not a post');\n}\n<\/code><\/pre>\n\n<p>This test will fail when I run it, but it makes me think about what I need to do to fix the error and how to do so in the best way.<\/p>\n\n<p>In this case, I need to make a request to the page that should render the text:<\/p>\n\n<pre><code class=\"php\">public function testOnlyPostNodesAreShown(): void {\n $this-&gt;drupalGet('\/blog');\n\n $assert = $this-&gt;assertSession();\n $assert-&gt;pageTextContains('Post one');\n $assert-&gt;pageTextContains('Post two');\n $assert-&gt;pageTextNotContains('This is not a post');\n}\n<\/code><\/pre>\n\n<p>This will still fail, as I also need to create the required posts:<\/p>\n\n<pre><code class=\"php\">public function testOnlyPostNodesAreShown(): void {\n PostBuilder::create()-&gt;setTitle('Post one')-&gt;getPost();\n PostBuilder::create()-&gt;setTitle('Post two')-&gt;getPost();\n\n $this-&gt;createNode([\n 'title' =&gt; 'This is not a post',\n 'type' =&gt; 'page',\n ]);\n\n $this-&gt;drupalGet('\/blog');\n\n $assert = $this-&gt;assertSession();\n $assert-&gt;pageTextContains('Post one');\n $assert-&gt;pageTextContains('Post two');\n $assert-&gt;pageTextNotContains('This is not a post');\n}\n<\/code><\/pre>\n\n<p>Now the test passes.<\/p>\n\n<p>Doing test-driven development keeps my code clean and minimal, and I find this approach keeps my test clean, too.<\/p>\n\n ",
"format": "full_html",
"processed": "\n <p>As well as <a href=\"https:\/\/www.oliverdavies.uk\/daily\/2024\/06\/03\/writing-comments-first\">writing comments first<\/a>, when writing tests, I sometimes like to write my tests backwards and start by writing the assertions first.<\/p>\n\n<p>I know what I want to assert in the test, so it's an easy place to start.<\/p>\n\n<p>I'll run it, see the error, fix it and continue working backwards.<\/p>\n\n<p>For example, I could start with this:<\/p>\n\n<pre><code class=\"php\">public function testOnlyPostNodesAreShown(): void {\n $assert = $this-&gt;assertSession();\n $assert-&gt;pageTextContains('Post one');\n $assert-&gt;pageTextContains('Post two');\n $assert-&gt;pageTextNotContains('This is not a post');\n}\n<\/code><\/pre>\n\n<p>This test will fail when I run it, but it makes me think about what I need to do to fix the error and how to do so in the best way.<\/p>\n\n<p>In this case, I need to make a request to the page that should render the text:<\/p>\n\n<pre><code class=\"php\">public function testOnlyPostNodesAreShown(): void {\n $this-&gt;drupalGet('\/blog');\n\n $assert = $this-&gt;assertSession();\n $assert-&gt;pageTextContains('Post one');\n $assert-&gt;pageTextContains('Post two');\n $assert-&gt;pageTextNotContains('This is not a post');\n}\n<\/code><\/pre>\n\n<p>This will still fail, as I also need to create the required posts:<\/p>\n\n<pre><code class=\"php\">public function testOnlyPostNodesAreShown(): void {\n PostBuilder::create()-&gt;setTitle('Post one')-&gt;getPost();\n PostBuilder::create()-&gt;setTitle('Post two')-&gt;getPost();\n\n $this-&gt;createNode([\n 'title' =&gt; 'This is not a post',\n 'type' =&gt; 'page',\n ]);\n\n $this-&gt;drupalGet('\/blog');\n\n $assert = $this-&gt;assertSession();\n $assert-&gt;pageTextContains('Post one');\n $assert-&gt;pageTextContains('Post two');\n $assert-&gt;pageTextNotContains('This is not a post');\n}\n<\/code><\/pre>\n\n<p>Now the test passes.<\/p>\n\n<p>Doing test-driven development keeps my code clean and minimal, and I find this approach keeps my test clean, too.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "2025-04-21T01:21:31+00:00",
"guid": null,
"hash": "3bdc81d1ff8f05f8b1beb3069dfdd370",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}