{"id":713,"date":"2009-02-23T18:07:08","date_gmt":"2009-02-23T17:07:08","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/02\/23\/using-naturalspec-to-create-spec-for-c-projects\/"},"modified":"2009-11-08T11:16:57","modified_gmt":"2009-11-08T10:16:57","slug":"using-naturalspec-to-create-spec-for-c-projects","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/02\/23\/using-naturalspec-to-create-spec-for-c-projects\/","title":{"rendered":"Using NaturalSpec to create a spec for C# projects (Updated 08.11.2009)"},"content":{"rendered":"<p>In my last two articles I gave an <a href=\"http:\/\/www.navision-blog.de\/2009\/02\/23\/introducing-naturalspec-a-dsl-for-testing-part-i\/\">introduction<\/a> in <a href=\"http:\/\/code.google.com\/p\/natural\/\">NaturalSpec<\/a> and showed <a href=\"http:\/\/www.navision-blog.de\/2009\/11\/08\/getting-started-with-naturalspec\/\">how to get started<\/a>. This time I will show how we can use <a href=\"http:\/\/code.google.com\/p\/natural\/\">NaturalSpec<\/a> to write automatically testable scenarios for C# projects.<\/p>\n<p>Like the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Test_Driven_Development\">TDD<\/a> principle \u201cWrite the tests first\u201d we should write our spec first and use the \u201cRed-Green-Refactor\u201d method.<\/p>\n<h5>&quot;Red&quot; \u2013 Create a spec scenario that fails<\/h5>\n<p>At first I created a F# class library project called \u201cSpec.CarSelling\u201d and added project references to <em>NaturalSpec.dll<\/em> and <em>nunit.framework.dll<\/em> (see <a href=\"http:\/\/www.navision-blog.de\/2009\/02\/23\/getting-started-with-naturalspec\/\">\u201cGetting started\u201d<\/a> for further explanations).<\/p>\n<p>Now I can write my first scenario:<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/ 1. define the module\n<\/span><span style=\"color: blue\">module <\/span>CarSpec<\/pre>\n<pre class=\"code\"><span style=\"color: green\">\/\/ 2. open the NaturalSpec namespace<br \/><\/span>\n<span style=\"color: blue\">open <\/span>NaturalSpec\n\n<span style=\"color: green\">\/\/ 3. open project namespace\n<\/span><span style=\"color: blue\">open <\/span>CarSellingLib\n\n<span style=\"color: green\">\/\/ 4. define a test context\n<\/span><span style=\"color: blue\">let <\/span>Bert = <span style=\"color: blue\">new <\/span>Dealer(<span style=\"color: maroon\">&quot;Bert&quot;<\/span>)\n\n<span style=\"color: green\">\/\/ 5. create a method in BDD-style\n<\/span><span style=\"color: blue\">let <\/span>selling_a_car_for amount (dealer:Dealer) =\n  printMethod amount\n  dealer.SellCar amount\n\n<span style=\"color: green\">\/\/ 6. create a scenario\n<\/span>[&lt;Scenario&gt;]\n<span style=\"color: blue\">let <\/span>When_selling_a_car_for_30000_it_should_equal_my_DreamCar() =\n  As Bert\n    |&gt; When selling_a_car_for 30000\n    |&gt; It should equal (<span style=\"color: blue\">new <\/span>Car(CarType.BMW, 200))\n    |&gt; Verify<\/pre>\n<p>At this stage the scenario is ready but doesn\u2019t compile. This means we are ready with the &quot;Red&quot;-stage.<\/p>\n<h5>&quot;Green&quot; \u2013 Make the test the pass<\/h5>\n<p>In order to get the test green we have to create a C# class library called <strong>CarSellingLib<\/strong> and define the enum <strong>CarType<\/strong> and the classes <strong>Dealer<\/strong> and <strong>Car<\/strong>. Sticking to the <a href=\"http:\/\/en.wikipedia.org\/wiki\/You_Ain%27t_Gonna_Need_It\">YAGNI-principle<\/a> we implement only the minimum to get the spec green (and ToString()-members for the output functionality).<\/p>\n<pre class=\"code\"><span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public enum <\/span><span style=\"color: #2b91af\">CarType\n    <\/span>{\n        BMW\n    }\n}<\/pre>\n<p>\n  <\/p>\n<pre class=\"code\"><span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public class <\/span><span style=\"color: #2b91af\">Car\n    <\/span>{\n        <span style=\"color: blue\">public <\/span>Car(<span style=\"color: #2b91af\">CarType <\/span>type, <span style=\"color: blue\">int <\/span>horsePower)\n        {\n            Type = type;\n            HorsePower = horsePower;\n        }\n\n        <span style=\"color: blue\">public <\/span><span style=\"color: #2b91af\">CarType <\/span>Type { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; }\n        <span style=\"color: blue\">public int <\/span>HorsePower { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; }\n\n        <span style=\"color: blue\">public override string <\/span>ToString()\n        {\n            <span style=\"color: blue\">return string<\/span>.Format(<span style=\"color: #a31515\">&quot;{0} ({1} HP)&quot;<\/span>, Type, HorsePower);\n        }\n\n        <span style=\"color: blue\">public override bool <\/span>Equals(<span style=\"color: blue\">object <\/span>obj)\n        {\n            <span style=\"color: blue\">var <\/span>y = obj <span style=\"color: blue\">as <\/span><span style=\"color: #2b91af\">Car<\/span>;\n            <span style=\"color: blue\">if<\/span>(y == <span style=\"color: blue\">null<\/span>) <span style=\"color: blue\">return false<\/span>;\n            <span style=\"color: blue\">return <\/span>Type == y.Type &amp;&amp; HorsePower == y.HorsePower;\n        }\n<span style=\"color: blue\">    <\/span>}\n}<br \/><\/pre>\n<pre class=\"code\"><span style=\"color: blue\">using <\/span>System;\n\n<span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public class <\/span><span style=\"color: #2b91af\">Dealer\n    <\/span>{\n        <span style=\"color: blue\">public <\/span>Dealer(<span style=\"color: blue\">string <\/span>name)\n        {\n            Name = name;\n        }\n\n        <span style=\"color: blue\">public string <\/span>Name { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; }\n\n        <span style=\"color: blue\">public <\/span><span style=\"color: #2b91af\">Car <\/span>SellCar(<span style=\"color: blue\">int <\/span>amount)\n        {\n            <span style=\"color: blue\">return new <\/span><span style=\"color: #2b91af\">Car<\/span>(<span style=\"color: #2b91af\">CarType<\/span>.BMW, 200);\n        }\n\n        <span style=\"color: blue\">public override string <\/span>ToString()\n        {\n            <span style=\"color: blue\">return <\/span>Name;\n        }\n    }\n}<\/pre>\n<p>\n  <br \/>When we add a project reference to our spec-project the UnitTests should pass and we have completed the &quot;Green&quot; step. (See &quot;<a href=\"http:\/\/www.navision-blog.de\/2009\/11\/08\/getting-started-with-naturalspec\/\">Getting started<\/a>&quot; if you don\u2019t know how to run the spec.) Now we can add some more scenarios to our spec: <\/p>\n<p><\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/ 1. define the module <br \/><\/span>\n<span style=\"color: blue\">module <\/span>CarSpec<\/pre>\n<pre class=\"code\"><span style=\"color: green\">\/\/ 2. open NaturalSpec-Namespace\n<\/span><span style=\"color: blue\">open <\/span>NaturalSpec\n\n<span style=\"color: green\">\/\/ 3. open project namespace\n<\/span><span style=\"color: blue\">open <\/span>CarSellingLib\n\n<span style=\"color: green\">\/\/ 4. define a test context\n<\/span><span style=\"color: blue\">let <\/span>Bert = <span style=\"color: blue\">new <\/span>Dealer(<span style=\"color: maroon\">&quot;Bert&quot;<\/span>)\n\n<span style=\"color: green\">\/\/ define reusable values\n<\/span><span style=\"color: blue\">let <\/span>DreamCar = <span style=\"color: blue\">new <\/span>Car(CarType.BMW, 200)\n<span style=\"color: blue\">let <\/span>LameCar = <span style=\"color: blue\">new <\/span>Car(CarType.Fiat, 45)\n\n<span style=\"color: green\">\/\/ 5. create a method in BDD-style\n<\/span><span style=\"color: blue\">let <\/span>selling_a_car_for amount (dealer:Dealer) =\n  printMethod amount\n  dealer.SellCar amount\n\n<span style=\"color: green\">\/\/ 6. create a scenario\n<\/span>[&lt;Scenario&gt;]\n<span style=\"color: blue\">let <\/span>When_selling_a_car_for_30000_it_should_equal_the_DreamCar() =\n  As Bert\n    |&gt; When selling_a_car_for 30000\n    |&gt; It should equal DreamCar\n    |&gt; It shouldn't equal LameCar\n    |&gt; Verify      \n\n[&lt;Scenario&gt;]\n<span style=\"color: blue\">let <\/span>When_selling_a_car_for_19000_it_should_equal_the_LameCar() =\n  As Bert\n    |&gt; When selling_a_car_for 19000\n    |&gt; It should equal LameCar\n    |&gt; It shouldn't equal DreamCar\n    |&gt; Verify<\/pre>\n<p>\n  <\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/ create a scenario that expects an error<\/span>\n[&lt;Scenario&gt;]\n[&lt;Fails_with <span style=\"color: #a31515\">&quot;Need more money&quot;<\/span>&gt;]\n<span style=\"color: blue\">let <\/span>When_selling_a_car_for_1000_it_should_fail_with_Need_More_Money() =\n  As Bert\n    |&gt; When selling_a_car_for 1000\n    |&gt; Verify<\/pre>\n<p>Now we are in the \u201cRed\u201d-Phase again.<\/p>\n<h5>&quot;Refactor&quot; &#8211; rearrange your code to eliminate duplication and follow patterns<\/h5>\n<p>After making the spec &quot;Green&quot; and doing some refactoring the project code could look like this:<\/p>\n<pre class=\"code\"><span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public enum <\/span><span style=\"color: #2b91af\">CarType\n    <\/span>{\n        Fiat,\n        BMW\n    }\n}<\/pre>\n<p>\n  <\/p>\n<pre class=\"code\"><span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public class <\/span><span style=\"color: #2b91af\">Car\n    <\/span>{\n        <span style=\"color: blue\">public <\/span>Car(<span style=\"color: #2b91af\">CarType <\/span>type, <span style=\"color: blue\">int <\/span>horsePower)\n        {\n            Type = type;\n            HorsePower = horsePower;\n        }\n\n        <span style=\"color: blue\">public <\/span><span style=\"color: #2b91af\">CarType <\/span>Type { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; }\n        <span style=\"color: blue\">public int <\/span>HorsePower { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; }\n\n        <span style=\"color: blue\"># region <\/span>ToString, Equals \n\n        <span style=\"color: blue\">public override string <\/span>ToString()\n        {\n            <span style=\"color: blue\">return string<\/span>.Format(<span style=\"color: #a31515\">&quot;{0} ({1} HP)&quot;<\/span>, Type, HorsePower);\n        }\n\n        <span style=\"color: blue\">public override bool <\/span>Equals(<span style=\"color: blue\">object <\/span>obj)\n        {\n            <span style=\"color: blue\">var <\/span>y = obj <span style=\"color: blue\">as <\/span><span style=\"color: #2b91af\">Car<\/span>;\n            <span style=\"color: blue\">if<\/span>(y == <span style=\"color: blue\">null<\/span>) <span style=\"color: blue\">return false<\/span>;\n            <span style=\"color: blue\">return <\/span>Type == y.Type &amp;&amp; HorsePower == y.HorsePower;\n        }\n\n        <span style=\"color: blue\">#endregion\n    <\/span>}\n}<\/pre>\n<p>\n  <\/p>\n<pre class=\"code\"><span style=\"color: blue\">using <\/span>System;\n\n<span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public class <\/span><span style=\"color: #2b91af\">Dealer\n    <\/span>{\n        <span style=\"color: blue\">public <\/span>Dealer(<span style=\"color: blue\">string <\/span>name)\n        {\n            Name = name;\n        }\n\n        <span style=\"color: blue\">public string <\/span>Name { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; }\n\n        <span style=\"color: blue\">public <\/span><span style=\"color: #2b91af\">Car <\/span>SellCar(<span style=\"color: blue\">int <\/span>amount)\n        {\n            <span style=\"color: blue\">if <\/span>(amount &gt; 20000)\n                <span style=\"color: blue\">return new <\/span><span style=\"color: #2b91af\">Car<\/span>(<span style=\"color: #2b91af\">CarType<\/span>.BMW, 200);\n\n            <span style=\"color: blue\">if <\/span>(amount &gt; 3000)\n                <span style=\"color: blue\">return new <\/span><span style=\"color: #2b91af\">Car<\/span>(<span style=\"color: #2b91af\">CarType<\/span>.Fiat, 45);\n\n            <span style=\"color: blue\">throw new <\/span><span style=\"color: #2b91af\">Exception<\/span>(<span style=\"color: #a31515\">&quot;Need more money&quot;<\/span>);\n        }\n\n        <span style=\"color: blue\">public override string <\/span>ToString()\n        {\n            <span style=\"color: blue\">return <\/span>Name;\n        }\n    }\n}<\/pre>\n<p>The spec output should look like the following:<\/p>\n<blockquote>\n<p>Scenario: When selling a car for 1000 it should fail with Need More Money<\/p>\n<p>&#8211; Should fail&#8230;<br \/>\n    &#8211; As Bert<br \/>\n    &#8211; When selling a car for 1000<\/p>\n<p>Scenario: When selling a car for 19000 it should equal the LameCar<\/p>\n<p>&#8211; As Bert<br \/>\n    &#8211; When selling a car for 19000<br \/>\n    =&gt; It should equal Fiat (45 HP)<br \/>\n    =&gt; It should not equal BMW (200 HP)<br \/>\n    ==&gt; OK<\/p>\n<p>Scenario: When selling a car for 30000 it should equal my DreamCar<\/p>\n<p>&#8211; As Bert<br \/>\n    &#8211; When selling a car for 30000<br \/>\n    =&gt; It should equal BMW (200 HP)<br \/>\n    ==&gt; OK<\/p>\n<p>Scenario: When selling a car for 30000 it should equal the DreamCar<\/p>\n<p>&#8211; As Bert<br \/>\n    &#8211; When selling a car for 30000<br \/>\n    =&gt; It should equal BMW (200 HP)<br \/>\n    =&gt; It should not equal Fiat (45 HP)<br \/>\n    >==&gt; OK<\/p>\n<p>4 passed, 0 failed, 0 skipped, took 1,81 seconds (NUnit 2.5).<\/p>\n<\/blockquote>\n<h5>Summary<\/h5>\n<p>I showed how we can use NaturalSpec for the Red-Green-Refactor process of C# projects and how easy it is to get a spec in natural language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last two articles I gave an introduction in NaturalSpec and showed how to get started. This time I will show how we can use NaturalSpec to write automatically testable scenarios for C# projects. Like the TDD principle \u201cWrite the tests first\u201d we should write our spec first and use the \u201cRed-Green-Refactor\u201d method. &quot;Red&quot; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448,524],"tags":[634,533,664,666,526,531],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/713"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=713"}],"version-history":[{"count":19,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/713\/revisions"}],"predecessor-version":[{"id":917,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/713\/revisions\/917"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=713"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}