{"_id":"544853a8c1b42e08005b82b6","project":"54471fc9e12a270800028adc","version":{"_id":"54471fc9e12a270800028adf","__v":10,"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"},"__v":1,"is_link":false,"user":"54471f91beb6320800da6f75","category":{"_id":"544854e74544c30800241f51","pages":["544853a8c1b42e08005b82b6","5447ecac8d7af31a00dd4119","5447ed6d0319802200fc0705","5447ef7a8d7af31a00dd4125","54485217c1b42e08005b82a6","5448536e4544c30800241f48","54485396c1b42e08005b82b2","547c7c5f78fd57080023c99c"],"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","__v":1,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-23T01:07:51.270Z","from_sync":false,"order":2,"slug":"basics","title":"Basics"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-23T01:02:32.217Z","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":1,"body":"SpecsFor follows the [Given, When, Then](http://martinfowler.com/bliki/GivenWhenThen.html) pattern of specifying tests.  You establish context or state in the Given phase, perform an action on the System Under Test in the When phase, then verify the state of things in the Then phase. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\tpublic class given_a_car_is_started_when_a_car_is_stopped : SpecsFor<Car>\\n\\t\\t{\\n\\t\\t\\tprotected override void Given()\\n\\t\\t\\t{\\n\\t\\t\\t\\t//Given: establish state, in this case, that the car is started.\\n\\t\\t\\t\\tSUT.Start();\\n\\t\\t\\t}\\n\\n\\t\\t\\tprotected override void When()\\n\\t\\t\\t{\\n\\t\\t\\t\\t//When: perform an action, in this case, stopping the car.\\n\\t\\t\\t\\tSUT.Stop();\\n\\t\\t\\t}\\n\\n\\t\\t\\t//Note that there can be (and usually are) multiple 'Then' test cases.\\n\\t\\t\\t[Test]\\n\\t\\t\\tpublic void then_the_car_is_stopped()\\n\\t\\t\\t{\\n\\t\\t\\t\\t//Then: the car is stopped.\\n\\t\\t\\t\\tSUT.IsStopped.ShouldBeTrue();\\n\\t\\t\\t}\\n\\n\\t\\t\\t[Test]\\n\\t\\t\\tpublic void then_the_engine_is_stopped()\\n\\t\\t\\t{\\n\\t\\t\\t\\t//Then: the engine is stopped.\\n\\t\\t\\t\\tSUT.Engine.IsStopped.ShouldBeTrue();\\n\\t\\t\\t}\\n\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNote that both the Given and When methods are optional.  You do not have to override them if you do not want to.  There may be times where you do not need to establish context first, in which case the Given is not needed.  There may also be times where encapsulating the action you are performing in the When method will actually slow things down.  In such cases, you can simply add test cases (Then methods) to your spec without specifying a Given or When method.","excerpt":"","slug":"given-when-then","type":"basic","title":"Given, When, Then"}
SpecsFor follows the [Given, When, Then](http://martinfowler.com/bliki/GivenWhenThen.html) pattern of specifying tests. You establish context or state in the Given phase, perform an action on the System Under Test in the When phase, then verify the state of things in the Then phase. [block:code] { "codes": [ { "code": "\t\tpublic class given_a_car_is_started_when_a_car_is_stopped : SpecsFor<Car>\n\t\t{\n\t\t\tprotected override void Given()\n\t\t\t{\n\t\t\t\t//Given: establish state, in this case, that the car is started.\n\t\t\t\tSUT.Start();\n\t\t\t}\n\n\t\t\tprotected override void When()\n\t\t\t{\n\t\t\t\t//When: perform an action, in this case, stopping the car.\n\t\t\t\tSUT.Stop();\n\t\t\t}\n\n\t\t\t//Note that there can be (and usually are) multiple 'Then' test cases.\n\t\t\t[Test]\n\t\t\tpublic void then_the_car_is_stopped()\n\t\t\t{\n\t\t\t\t//Then: the car is stopped.\n\t\t\t\tSUT.IsStopped.ShouldBeTrue();\n\t\t\t}\n\n\t\t\t[Test]\n\t\t\tpublic void then_the_engine_is_stopped()\n\t\t\t{\n\t\t\t\t//Then: the engine is stopped.\n\t\t\t\tSUT.Engine.IsStopped.ShouldBeTrue();\n\t\t\t}\n\t\t}", "language": "csharp" } ] } [/block] Note that both the Given and When methods are optional. You do not have to override them if you do not want to. There may be times where you do not need to establish context first, in which case the Given is not needed. There may also be times where encapsulating the action you are performing in the When method will actually slow things down. In such cases, you can simply add test cases (Then methods) to your spec without specifying a Given or When method.