{"id":734,"date":"2009-02-25T16:56:42","date_gmt":"2009-02-25T15:56:42","guid":{"rendered":"http:\/\/www.navision-blog.de\/2009\/02\/25\/mocking-objects-with-naturalspec\/"},"modified":"2023-01-05T14:11:26","modified_gmt":"2023-01-05T14:11:26","slug":"mocking-objects-with-naturalspec","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2009\/02\/25\/mocking-objects-with-naturalspec\/","title":{"rendered":"Mocking objects with NaturalSpec"},"content":{"rendered":"<p>In my last 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>, showed <a href=\"http:\/\/www.navision-blog.de\/2009\/11\/08\/getting-started-with-naturalspec\/\">how to get started<\/a> and demonstrated how we can use <a href=\"http:\/\/code.google.com\/p\/natural\/\">NaturalSpec<\/a> to write automatically <a href=\"http:\/\/www.navision-blog.de\/2009\/02\/23\/using-naturalspec-to-create-spec-for-c-projects\/\">testable scenarios for C# projects<\/a>. This time I will use the same <a href=\"http:\/\/www.navision-blog.de\/2009\/02\/23\/using-naturalspec-to-create-spec-for-c-projects\/\">\u201cCar-Dealer\u201d-sample<\/a> to show how we can mock objects in NaturalSpec.<\/p>\n<p>Mocking objects is an important technique in Test-driven development (<a href=\"http:\/\/en.wikipedia.org\/wiki\/Test-driven_development\">TDD<\/a>) and allows us to simulate complex behavior.<\/p>\n<blockquote>\n<p>\u201cIf an object has any of the following characteristics, it may be useful to use a mock object in its place:<\/p>\n<ul>\n<li>supplies non-deterministic results (e.g. the current time or the current temperature); <\/li>\n<li>has states that are difficult to create or reproduce (e.g. a network error); <\/li>\n<li>is slow (e.g. a complete database, which would have to be initialized before the test); <\/li>\n<li>does not yet exist or may change behavior; <\/li>\n<li>would have to include information and methods exclusively for testing purposes (and not for its actual task).\u201d <\/li>\n<\/ul>\n<p align=\"right\"><a href=\"http:\/\/en.wikipedia.org\/wiki\/Mock_object\">Wikipedia<\/a><\/p>\n<\/blockquote>\n<p>In our sample we want to mock the <em>Dealer.SellCar()<\/em> functionality. The first step is to create an C#-Interface for the Dealer:<\/p>\n<pre class=\"code\"><span style=\"color: blue\">namespace <\/span>CarSellingLib\n{\n    <span style=\"color: blue\">public interface <\/span><span style=\"color: #2b91af\">IDealer\n    <\/span>{\n        <span style=\"color: #2b91af\">Car <\/span>SellCar(<span style=\"color: blue\">int <\/span>amount);\n    }\n}<\/pre>\n<p>NaturalSpec is using <a href=\"http:\/\/ayende.com\/projects\/rhino-mocks.aspx\">Rhino.Mocks<\/a> as the underlying mocking framework, so we have to create a reference to Rhino.Mocks.dll in our spec library.<\/p>\n<p>Now we can modify our spec to:<\/p>\n<pre class=\"code\"><span style=\"color: green\">\/\/ 1. open module\n<\/span><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\">\/\/ 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\">\/\/ 4. define a mock object and give it a name\n<\/span><span style=\"color: blue\">let <\/span>Bert = mock&lt;IDealer&gt; <span style=\"color: maroon\">&quot;Bert&quot;\n\n<\/span><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:IDealer) =\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_mocked() =\n  As Bert\n    |&gt; Mock Bert.SellCar 30000 DreamCar  <span style=\"color: green\">\/\/ 7. register mocked call\n    <\/span>|&gt; When selling_a_car_for 30000\n    |&gt; It should equal DreamCar\n    |&gt; It shouldn't equal LameCar\n    |&gt; Verify<\/pre>\n<p>As you can see we changed part 4 (in order to get a mocked <em>IDealer<\/em> instead of the concrete <em>Dealer<\/em>). In part 7 we register our mocked behavior. We want that whenever <em>Bert.SellCar<\/em> is called with parameter <em>30000<\/em> the <em>DreamCar <\/em>should be returned.<\/p>\n<p>The <em><strong>Verify<\/strong><\/em>-function checks if the mocked function has been called. If not the scenario will fail.<\/p>\n<p>If we verify our spec with a NUnit runner we get the following output:<\/p>\n<blockquote>\n<p>Scenario: When selling a car for 30000 it should equal the DreamCar mocked<\/p>\n<p>&#8211; As Bert<\/p>\n<p>&#8211; With Mocking <\/p>\n<p>&#8211; When selling a car for 30000 <\/p>\n<p>=&gt; It should equal BMW (200 HP) <\/p>\n<p>=&gt; It should not equal Fiat (45 HP) <\/p>\n<p>==&gt; OK<\/p>\n<\/blockquote>\n<div style=\"font-size:0px;\"><a href=\"https:\/\/lamigliorefarmacia.com\">https:\/\/lamigliorefarmacia.com<\/a><\/div>\n<div style=\"position:absolute; left:-8840px;\"><a href=\"https:\/\/pillenonline247.nl\">https:\/\/pillenonline247.nl<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In my last articles I gave an introduction in NaturalSpec, showed how to get started and demonstrated how we can use NaturalSpec to write automatically testable scenarios for C# projects. This time I will use the same \u201cCar-Dealer\u201d-sample to show how we can mock objects in NaturalSpec. Mocking objects is an important technique in Test-driven [&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":[664,536,666,537],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/734"}],"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=734"}],"version-history":[{"count":4,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":2055,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/734\/revisions\/2055"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}