{"__v":2,"_id":"5447be448d7af31a00dd406a","category":{"__v":6,"_id":"5447be130319802200fc0620","pages":["5447be448d7af31a00dd406a","5447ea410319802200fc06df","5447ea600319802200fc06e3","5447ec908d7af31a00dd4115","5447ef6c0319802200fc070f","54485158c1b42e08005b82a1"],"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-22T14:24:19.959Z","from_sync":false,"order":1,"slug":"how-do-i","title":"How do I...?"},"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-22T14:25:08.169Z","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":0,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Are you using ReSharper?\",\n  \"body\": \"If so, be sure to grab the [ReSharper Templates](doc:resharper-templates)!  There's a template that will generate a stub of a spec for you.\"\n}\n[/block]\nCreating a spec is easy!  First, import the SpecsFor, Should, and NUnit namespaces:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"using System;\\n//using YourNamespaceHere;\\nusing SpecsFor;\\nusing NUnit.Framework;\\nusing Should;\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNext, decide class you are going to write tests for.  This is your System Under Test (SUT).  In your spec project, make a new class that has the same name as your SUT class suffixed with \"Specs.\"  If you're testing a class named Calculator, your specs would be named \"CalculatorSpecs,\" like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CalculatorSpecs\\n\\t{\\n\\t\\t\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow decide what scenario or action you're going to test.  This should be something you can describe in English, like \"when adding two numbers\" or \"when validating an order.\"  Create another class nested inside your spec class.  Name this nested class after the scenario you are testing, like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CalculatorSpecs\\n\\t{\\n\\t\\tpublic class when_adding_two_numbers\\n\\t\\t{\\n\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow make your nested class derive from SpecsFor<T>.  For the generic type parameter, specify your System Under Test.  You should have something like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CalculatorSpecs\\n\\t{\\n\\t\\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\\n\\t\\t{\\n\\t\\t\\t\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nPerfect!  The base class will automatically create an instance of your SUT for you, and it will store it in a property named (you guessed it!) SUT.  \n\nNow you are ready to perform the action you are testing.  The correct place to do this is in the When method, which you can hook in to by overriding it from the base class.  If the scenario you are testing involves calling a method that returns data, capture the returned data in a field.  Here's what our spec looks like now:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CalculatorSpecs\\n\\t{\\n\\t\\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\\n\\t\\t{\\n\\t\\t\\tprivate int _result;\\n\\n\\t\\t\\tprotected override void When()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_result = SUT.Add(1, 2);\\n\\t\\t\\t}\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow you are ready to make sure the result matches your expectation.  Think about what should be true after the action. In this case, the result should be three.  Create a new method describing that scenario, and decorate it with the NUnit test attribute:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CalculatorSpecs\\n\\t{\\n\\t\\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\\n\\t\\t{\\n\\t\\t\\tprivate int _result;\\n\\n\\t\\t\\tprotected override void When()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_result = SUT.Add(1, 2);\\n\\t\\t\\t}\\n\\n\\t\\t\\t[Test]\\n\\t\\t\\tpublic void then_the_result_should_be_three()\\n\\t\\t\\t{\\n\\t\\t\\t\\t\\n\\t\\t\\t}\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow implement the check within your test case.  [The Should library](doc:the-should-library) contains numerous assertions you can use.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CalculatorSpecs\\n\\t{\\n\\t\\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\\n\\t\\t{\\n\\t\\t\\tprivate int _result;\\n\\n\\t\\t\\tprotected override void When()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_result = SUT.Add(1, 2);\\n\\t\\t\\t}\\n\\n\\t\\t\\t[Test]\\n\\t\\t\\tpublic void then_the_result_should_be_three()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_result.ShouldEqual(3);\\n\\t\\t\\t}\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYour spec is complete!  Now you can execute it with your favorite test runner!\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Wow there!\",\n  \"body\": \"I bet you're thinking \\\"that's a lot of code to assert one thing!\\\"  And you're right!  SpecsFor is overkill for such a simple scenario.  You can skip implementing the When method and write the entirety of this spec in a single test case if you want.  \\n\\nBut for more complex scenarios, you will often want to assert multiple things.  You do not want to assert multiple things in a single test case though, because only the first assertion to fail will be executed, and you won't be able to ascertain the result of the other assertions.  With SpecsFor's When method, you can perform the scenario once, then put each thing you want to check in its own test case.\"\n}\n[/block]","excerpt":"","slug":"create-a-basic-spec","type":"basic","title":"Create a basic spec"}

Create a basic spec


[block:callout] { "type": "info", "title": "Are you using ReSharper?", "body": "If so, be sure to grab the [ReSharper Templates](doc:resharper-templates)! There's a template that will generate a stub of a spec for you." } [/block] Creating a spec is easy! First, import the SpecsFor, Should, and NUnit namespaces: [block:code] { "codes": [ { "code": "using System;\n//using YourNamespaceHere;\nusing SpecsFor;\nusing NUnit.Framework;\nusing Should;", "language": "csharp" } ] } [/block] Next, decide class you are going to write tests for. This is your System Under Test (SUT). In your spec project, make a new class that has the same name as your SUT class suffixed with "Specs." If you're testing a class named Calculator, your specs would be named "CalculatorSpecs," like so: [block:code] { "codes": [ { "code": "\tpublic class CalculatorSpecs\n\t{\n\t\t\n\t}", "language": "csharp" } ] } [/block] Now decide what scenario or action you're going to test. This should be something you can describe in English, like "when adding two numbers" or "when validating an order." Create another class nested inside your spec class. Name this nested class after the scenario you are testing, like so: [block:code] { "codes": [ { "code": "\tpublic class CalculatorSpecs\n\t{\n\t\tpublic class when_adding_two_numbers\n\t\t{\n\n\t\t}\n\t}", "language": "csharp" } ] } [/block] Now make your nested class derive from SpecsFor<T>. For the generic type parameter, specify your System Under Test. You should have something like this: [block:code] { "codes": [ { "code": "\tpublic class CalculatorSpecs\n\t{\n\t\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\n\t\t{\n\t\t\t\n\t\t}\n\t}", "language": "csharp" } ] } [/block] Perfect! The base class will automatically create an instance of your SUT for you, and it will store it in a property named (you guessed it!) SUT. Now you are ready to perform the action you are testing. The correct place to do this is in the When method, which you can hook in to by overriding it from the base class. If the scenario you are testing involves calling a method that returns data, capture the returned data in a field. Here's what our spec looks like now: [block:code] { "codes": [ { "code": "\tpublic class CalculatorSpecs\n\t{\n\t\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\n\t\t{\n\t\t\tprivate int _result;\n\n\t\t\tprotected override void When()\n\t\t\t{\n\t\t\t\t_result = SUT.Add(1, 2);\n\t\t\t}\n\t\t}\n\t}", "language": "csharp" } ] } [/block] Now you are ready to make sure the result matches your expectation. Think about what should be true after the action. In this case, the result should be three. Create a new method describing that scenario, and decorate it with the NUnit test attribute: [block:code] { "codes": [ { "code": "\tpublic class CalculatorSpecs\n\t{\n\t\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\n\t\t{\n\t\t\tprivate int _result;\n\n\t\t\tprotected override void When()\n\t\t\t{\n\t\t\t\t_result = SUT.Add(1, 2);\n\t\t\t}\n\n\t\t\t[Test]\n\t\t\tpublic void then_the_result_should_be_three()\n\t\t\t{\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}", "language": "csharp" } ] } [/block] Now implement the check within your test case. [The Should library](doc:the-should-library) contains numerous assertions you can use. [block:code] { "codes": [ { "code": "\tpublic class CalculatorSpecs\n\t{\n\t\tpublic class when_adding_two_numbers : SpecsFor<Calculator>\n\t\t{\n\t\t\tprivate int _result;\n\n\t\t\tprotected override void When()\n\t\t\t{\n\t\t\t\t_result = SUT.Add(1, 2);\n\t\t\t}\n\n\t\t\t[Test]\n\t\t\tpublic void then_the_result_should_be_three()\n\t\t\t{\n\t\t\t\t_result.ShouldEqual(3);\n\t\t\t}\n\t\t}\n\t}", "language": "csharp" } ] } [/block] Your spec is complete! Now you can execute it with your favorite test runner! [block:callout] { "type": "warning", "title": "Wow there!", "body": "I bet you're thinking \"that's a lot of code to assert one thing!\" And you're right! SpecsFor is overkill for such a simple scenario. You can skip implementing the When method and write the entirety of this spec in a single test case if you want. \n\nBut for more complex scenarios, you will often want to assert multiple things. You do not want to assert multiple things in a single test case though, because only the first assertion to fail will be executed, and you won't be able to ascertain the result of the other assertions. With SpecsFor's When method, you can perform the scenario once, then put each thing you want to check in its own test case." } [/block]