{"__v":2,"_id":"544854904544c30800241f4e","category":{"__v":1,"_id":"544854504544c30800241f4d","pages":["544854904544c30800241f4e"],"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-23T01:05:20.442Z","from_sync":false,"order":5,"slug":"sharing-context","title":"Sharing Context"},"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":["590372b3902c2f19004d3a1b"],"next":{"pages":[],"description":""},"createdAt":"2014-10-23T01:06:24.528Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"basic_auth":false,"results":{"codes":[]},"settings":"","try":true,"auth":"never","params":[],"url":""},"isReference":false,"order":0,"body":"Context classes allow you to create encapsulated behaviors that can be consumed in a spec. You can easily reuse context across test fixtures in a variety of ways. There's the typical inheritance-based approach:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public class OrderProcessorSpecs\\n{\\n\\tpublic class when_processing_an_order : given.the_item_is_available\\n\\t{\\n\\t\\tprivate OrderResult _result;\\n\\n\\t\\tprotected override void When()\\n\\t\\t{\\n\\t\\t\\t_result = SUT.Process(new Order { PartNumber = \\\"TestPart\\\", Quantity = 10 });\\n\\t\\t}\\n\\n\\t\\t[Test]\\n\\t\\tpublic void then_the_order_is_accepted()\\n\\t\\t{\\n\\t\\t\\t_result.WasAccepted.ShouldBeTrue();\\t\\n\\t\\t}\\n\\n\\t\\t[Test]\\n\\t\\tpublic void then_it_checks_the_inventory()\\n\\t\\t{\\n\\t\\t\\tGetMockFor<IInventory>().Verify();\\n\\t\\t}\\n\\n\\t\\t[Test]\\n\\t\\tpublic void then_it_raises_an_order_submitted_event()\\n\\t\\t{\\n\\t\\t\\tGetMockFor<IPublisher>()\\n\\t\\t\\t\\t.Verify(p => p.Publish(It.Is<OrderSubmitted>(o => o.OrderNumber == _result.OrderNumber)));\\n\\t\\t}\\n\\t}\\n\\n\\t\\n\\tpublic class when_processing_an_order_with_a_negative_quantity : given.the_item_is_available\\n\\t{\\n\\t\\tprivate OrderResult _result;\\n\\n\\t\\tprotected override void When()\\n\\t\\t{\\n\\t\\t\\t_result = SUT.Process(new Order{ PartNumber = \\\"TestPart\\\", Quantity = -1});\\n\\t\\t}\\n\\n\\t\\t[Test]\\n\\t\\tpublic void then_the_order_is_rejected()\\n\\t\\t{\\n\\t\\t\\t_result.WasAccepted.ShouldBeFalse();\\n\\t\\t}\\n\\n\\t\\t[Test]\\n\\t\\tpublic void then_it_does_not_check_the_inventory()\\n\\t\\t{\\n\\t\\t\\tGetMockFor<IInventory>()\\n\\t\\t\\t\\t.Verify(i => i.IsQuantityAvailable(\\\"TestPart\\\", -1), Times.Never());\\n\\t\\t}\\n\\n\\t\\t[Test]\\n\\t\\tpublic void then_it_does_not_raise_an_order_submitted_event()\\n\\t\\t{\\n\\t\\t\\tGetMockFor<IPublisher>()\\n\\t\\t\\t\\t.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never());\\n\\t\\t}\\n\\t}\\n\\n\\tpublic class given\\n\\t{\\n\\t\\tpublic class the_item_is_available : SpecsFor<OrderProcessor>\\n\\t\\t{\\n\\t\\t\\tprotected override void Given()\\n\\t\\t\\t{\\n\\t\\t\\t\\tGetMockFor<IInventory>()\\n\\t\\t\\t\\t\\t.Setup(i => i.IsQuantityAvailable(\\\"TestPart\\\", 10))\\n\\t\\t\\t\\t\\t.Returns(true)\\n\\t\\t\\t\\t\\t.Verifiable();\\n\\t\\t\\t}\\n\\t\\t}\\n\\t}\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"intro-to-context-classes","type":"basic","title":"Intro to context classes"}

Intro to context classes


Context classes allow you to create encapsulated behaviors that can be consumed in a spec. You can easily reuse context across test fixtures in a variety of ways. There's the typical inheritance-based approach: [block:code] { "codes": [ { "code": "public class OrderProcessorSpecs\n{\n\tpublic class when_processing_an_order : given.the_item_is_available\n\t{\n\t\tprivate OrderResult _result;\n\n\t\tprotected override void When()\n\t\t{\n\t\t\t_result = SUT.Process(new Order { PartNumber = \"TestPart\", Quantity = 10 });\n\t\t}\n\n\t\t[Test]\n\t\tpublic void then_the_order_is_accepted()\n\t\t{\n\t\t\t_result.WasAccepted.ShouldBeTrue();\t\n\t\t}\n\n\t\t[Test]\n\t\tpublic void then_it_checks_the_inventory()\n\t\t{\n\t\t\tGetMockFor<IInventory>().Verify();\n\t\t}\n\n\t\t[Test]\n\t\tpublic void then_it_raises_an_order_submitted_event()\n\t\t{\n\t\t\tGetMockFor<IPublisher>()\n\t\t\t\t.Verify(p => p.Publish(It.Is<OrderSubmitted>(o => o.OrderNumber == _result.OrderNumber)));\n\t\t}\n\t}\n\n\t\n\tpublic class when_processing_an_order_with_a_negative_quantity : given.the_item_is_available\n\t{\n\t\tprivate OrderResult _result;\n\n\t\tprotected override void When()\n\t\t{\n\t\t\t_result = SUT.Process(new Order{ PartNumber = \"TestPart\", Quantity = -1});\n\t\t}\n\n\t\t[Test]\n\t\tpublic void then_the_order_is_rejected()\n\t\t{\n\t\t\t_result.WasAccepted.ShouldBeFalse();\n\t\t}\n\n\t\t[Test]\n\t\tpublic void then_it_does_not_check_the_inventory()\n\t\t{\n\t\t\tGetMockFor<IInventory>()\n\t\t\t\t.Verify(i => i.IsQuantityAvailable(\"TestPart\", -1), Times.Never());\n\t\t}\n\n\t\t[Test]\n\t\tpublic void then_it_does_not_raise_an_order_submitted_event()\n\t\t{\n\t\t\tGetMockFor<IPublisher>()\n\t\t\t\t.Verify(p => p.Publish(It.IsAny<OrderSubmitted>()), Times.Never());\n\t\t}\n\t}\n\n\tpublic class given\n\t{\n\t\tpublic class the_item_is_available : SpecsFor<OrderProcessor>\n\t\t{\n\t\t\tprotected override void Given()\n\t\t\t{\n\t\t\t\tGetMockFor<IInventory>()\n\t\t\t\t\t.Setup(i => i.IsQuantityAvailable(\"TestPart\", 10))\n\t\t\t\t\t.Returns(true)\n\t\t\t\t\t.Verifiable();\n\t\t\t}\n\t\t}\n\t}\n}", "language": "csharp" } ] } [/block]