{"__v":1,"_id":"54485396c1b42e08005b82b2","category":{"__v":1,"_id":"544854e74544c30800241f51","pages":["544853a8c1b42e08005b82b6","5447ecac8d7af31a00dd4119","5447ed6d0319802200fc0705","5447ef7a8d7af31a00dd4125","54485217c1b42e08005b82a6","5448536e4544c30800241f48","54485396c1b42e08005b82b2","547c7c5f78fd57080023c99c"],"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-23T01:07:51.270Z","from_sync":false,"order":2,"slug":"basics","title":"Basics"},"is_link":false,"project":"54471fc9e12a270800028adc","user":"54471f91beb6320800da6f75","version":{"__v":10,"_id":"54471fc9e12a270800028adf","project":"54471fc9e12a270800028adc","createdAt":"2014-10-22T03:08:57.750Z","releaseDate":"2014-10-22T03:08:57.750Z","categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-23T01:02:14.922Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"basic_auth":false,"results":{"codes":[]},"try":true,"auth":"never","params":[],"url":""},"isReference":false,"order":7,"body":"In the [Given, When, Then](doc:given-when-then) setup, the Then step is for you that the action you performed against the [System Under Test (SUT)](doc:the-system-under-test-sut) had the desired outcome(s).  \n\nConsider our simple spec again: \n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Given a car is started, when the car is stopped, then the engine is stopped.\"\n}\n[/block]\nUnlike Given and When, there is no Then method to override in SpecsFor.  Instead, you create test cases for each outcome you wish to verify, like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[Test]\\npublic void then_the_engine_is_stopped()\\n{\\n  //Then: the engine is stopped.\\n  SUT.Engine.IsStopped.ShouldBeTrue();\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"BE CAREFUL!\",\n  \"body\": \"Be sure you decorate each of your test cases with the NUnit Test attribute.  If you don't, NUnit won't see your test cases, and they won't be executed.\"\n}\n[/block]\n\nThe above spec only had a single outcome to verify, but it's not uncommon for specs to have multiple things to check:\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Given a car is started, when the car is stopped..\\n   then the car is stopped.\\n   then the engine is stopped.\"\n}\n[/block]\nThe full SpecsFor implementation of this spec would look like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public class given_a_car_is_started_when_a_car_is_stopped : SpecsFor<Car>\\n{\\n  protected override void Given()\\n  {\\n    //Given: establish state, in this case, that the car is started.\\n    SUT.Start();\\n  }\\n\\n  protected override void When()\\n  {\\n    //When: perform an action, in this case, stopping the car.\\n    SUT.Stop();\\n  }\\n\\n  //Note that there can be (and usually are) multiple 'Then' test cases.\\n  [Test]\\n  public void then_the_car_is_stopped()\\n  {\\n    //Then: the car is stopped.\\n    SUT.IsStopped.ShouldBeTrue();\\n  }\\n\\n  [Test]\\n  public void then_the_engine_is_stopped()\\n  {\\n    //Then: the engine is stopped.\\n    SUT.Engine.IsStopped.ShouldBeTrue();\\n  }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"verifying-with-then","type":"basic","title":"Verifying with 'Then'"}

Verifying with 'Then'


In the [Given, When, Then](doc:given-when-then) setup, the Then step is for you that the action you performed against the [System Under Test (SUT)](doc:the-system-under-test-sut) had the desired outcome(s). Consider our simple spec again: [block:callout] { "type": "info", "body": "Given a car is started, when the car is stopped, then the engine is stopped." } [/block] Unlike Given and When, there is no Then method to override in SpecsFor. Instead, you create test cases for each outcome you wish to verify, like so: [block:code] { "codes": [ { "code": "[Test]\npublic void then_the_engine_is_stopped()\n{\n //Then: the engine is stopped.\n SUT.Engine.IsStopped.ShouldBeTrue();\n}", "language": "csharp" } ] } [/block] [block:callout] { "type": "warning", "title": "BE CAREFUL!", "body": "Be sure you decorate each of your test cases with the NUnit Test attribute. If you don't, NUnit won't see your test cases, and they won't be executed." } [/block] The above spec only had a single outcome to verify, but it's not uncommon for specs to have multiple things to check: [block:callout] { "type": "info", "body": "Given a car is started, when the car is stopped..\n then the car is stopped.\n then the engine is stopped." } [/block] The full SpecsFor implementation of this spec would look like this: [block:code] { "codes": [ { "code": "public class given_a_car_is_started_when_a_car_is_stopped : SpecsFor<Car>\n{\n protected override void Given()\n {\n //Given: establish state, in this case, that the car is started.\n SUT.Start();\n }\n\n protected override void When()\n {\n //When: perform an action, in this case, stopping the car.\n SUT.Stop();\n }\n\n //Note that there can be (and usually are) multiple 'Then' test cases.\n [Test]\n public void then_the_car_is_stopped()\n {\n //Then: the car is stopped.\n SUT.IsStopped.ShouldBeTrue();\n }\n\n [Test]\n public void then_the_engine_is_stopped()\n {\n //Then: the engine is stopped.\n SUT.Engine.IsStopped.ShouldBeTrue();\n }\n}", "language": "csharp" } ] } [/block]