<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://community.sharpdevelop.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Matt Ward : MSpec</title><link>http://community.sharpdevelop.net/blogs/mattward/archive/tags/MSpec/default.aspx</link><description>Tags: MSpec</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 SP2 (Build: 31113.47)</generator><item><title>Machine.Specifications (MSpec)</title><link>http://community.sharpdevelop.net/blogs/mattward/archive/2011/11/09/MachineSpecifications.aspx</link><pubDate>Wed, 09 Nov 2011 22:07:00 GMT</pubDate><guid isPermaLink="false">1b90d1c1-04e6-45b0-b51d-b665527d49b9:37615</guid><dc:creator>MattWard</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://community.sharpdevelop.net/blogs/mattward/rsscomments.aspx?PostID=37615</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://community.sharpdevelop.net/blogs/mattward/commentapi.aspx?PostID=37615</wfw:comment><comments>http://community.sharpdevelop.net/blogs/mattward/archive/2011/11/09/MachineSpecifications.aspx#comments</comments><description>&lt;p&gt;Here is a preview of a new feature that is planned for 
    SharpDevelop - integrated support for Machine.Specifications.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://community.sharpdevelop.net/blogs/mattward/articles/machine.aspx"&gt;Machine.Specifications&lt;/a&gt;, or MSpec for short, is a 
    &lt;a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development"&gt;
    Behaviour Driven Development&lt;/a&gt; framework for .NET created by 
    &lt;a href="http://codebetter.com/aaronjensen/2008/05/08/introducing-machine-specifications-or-mspec-for-short/"&gt;Aaron Jensen&lt;/a&gt; which takes its inspiration from 
    &lt;a href="http://code.google.com/p/specunit-net/"&gt;SpecUnit.NET&lt;/a&gt; 
    and &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Support for MSpec has been added thanks to 
    &lt;a href="http://blog.trecio.cba.pl"&gt;Tomasz Tretkowski&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So let us take a look at how the MSpec integration works in 
    SharpDevelop.&lt;/p&gt;
&lt;h2&gt;MSpec Integration&lt;/h2&gt;
&lt;p&gt;The first thing you will need to do is add a reference to MSpec 
    in the project that will contain your specifications. The easiest 
    way to do this is to use NuGet. Select your project, right click 
    and select &lt;b&gt;Manage Packages&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Manage Packages context menu" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecManagePackagesContextMenu.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;Search for the &lt;b&gt;Machine.Specifications&lt;/b&gt; package and then 
    click the &lt;b&gt;Add&lt;/b&gt; button to add the NuGet package to your 
    project.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec NuGet package selected in Manage Packages dialog" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecPackageInManagePackagesDialog.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;This will add a Machine.Specifications assembly reference to 
    your project.&lt;/p&gt;
&lt;p&gt;Now let us create our first specification. We are going to 
    create a specification which is based on an 
    &lt;a href="http://community.sharpdevelop.net/blogs/mattward/articles/Machine.Specifications.aspx"&gt;example&lt;/a&gt; provided with the MSpec source code. The specification 
    is for transferring an amount of money between two bank accounts 
    and is shown below.&lt;/p&gt;
&lt;pre&gt;using System; 
using Machine.Specifications; 
 
namespace Banking.Tests 
{ 
    [Subject(typeof(Account), &amp;quot;Funds transfer&amp;quot;)] 
    public class when_transferring_between_two_accounts 
    { 
        static Account fromAccount; 
        static Account toAccount; 
         
        Establish context = () =&amp;gt; { 
            fromAccount = new Account { Balance = 1m }; 
            toAccount = new Account { Balance = 1m }; 
        }; 
         
        Because of = () =&amp;gt; 
            fromAccount.Transfer(1m, toAccount); 
         
        It should_debit_the_from_account_by_the_amount_transferred = () =&amp;gt; 
            fromAccount.Balance.ShouldEqual(0m); 
         
        It should_credit_the_to_account_by_the_amount_transferred = () =&amp;gt; 
            toAccount.Balance.ShouldEqual(2m); 
    } 
}&lt;/pre&gt;
&lt;p&gt;MSpec uses &lt;b&gt;Establish&lt;/b&gt;/&lt;b&gt;Because&lt;/b&gt;/&lt;b&gt;It&lt;/b&gt; which is 
    equivalent to &lt;b&gt;Given&lt;/b&gt;/&lt;b&gt;When&lt;/b&gt;/&lt;b&gt;Then&lt;/b&gt; that is used 
    with SpecFlow. &lt;b&gt;Establish&lt;/b&gt; is used to setup the initial state. 
    &lt;b&gt;Because&lt;/b&gt; is used to define the event/action that you are 
    testing. &lt;b&gt;It&lt;/b&gt; is used to test the final state and see if it 
    matches what is expected.&lt;/p&gt;
&lt;p&gt;The Account class that is used by this specification is shown 
    below.&lt;/p&gt;
&lt;pre&gt;using System; 
 
namespace Banking 
{ 
    public class Account 
    { 
        public decimal Balance { get; set; } 
        
        public void Transfer(decimal amount, Account toAccount) 
        {             
        }
    } 
}&lt;/pre&gt;
&lt;p&gt;Specifications are displayed in the &lt;b&gt;Unit Tests&lt;/b&gt; window. 
    This window can be opened by selecting &lt;b&gt;Unit Tests&lt;/b&gt; from the 
    &lt;b&gt;View&lt;/b&gt; | &lt;b&gt;Tools&lt;/b&gt; menu. The specification we have defined 
    will be displayed in the Unit Test window as shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec tests in Unit Tests window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecTestsInUnitTestsWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;The Unit Tests window will update as the specification is 
    written in the same way it does when you are writing NUnit 
    tests.&lt;/p&gt;
&lt;p&gt;To run MSpec you can right click a specification and select 
    &lt;b&gt;Run tests&lt;/b&gt; or you can click the toolbar buttons at the top of 
    the Unit Tests window.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Run tests context menu in Unit Tests window" src="http://community.sharpdevelop.net/photos/mattward/images/original/RunMSpecTestsInUnitTestsWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;Failures will be highlighted in Red in the Unit Tests window.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec failures in Unit Tests window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecFailuresInUnitTestsWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;The output from MSpec is displayed in the &lt;b&gt;Output&lt;/b&gt; 
    window.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec runner output in Output window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecOutputInOutputWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;Failures are also added to the &lt;b&gt;Errors&lt;/b&gt; window. Clicking on 
    an error will take you directly to the source code for the failing 
    test.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec failures in Errors window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecFailuresInOutputWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;Now we have to fix the failures by implementing the 
    Account&amp;#39;s Transfer method. The updated Transfer method is 
    shown below:&lt;/p&gt;
&lt;pre&gt;public void Transfer(decimal amount, Account toAccount) 
{ 
    Balance -= amount; 
    toAccount.Balance += amount; 
}&lt;/pre&gt;
&lt;p&gt;After making this change and running MSpec again everything 
    passes, as shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec passes shown in green in Unit Tests window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecPassesInUnitTestsWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec success output in Output Window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecPassedTestsOutputInOutputWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;That completes the quick introduction to using MSpec with 
    SharpDevelop. Now let us take a look at MSpec Behaviours.&lt;/p&gt;
&lt;h2&gt;MSpec Behaviours&lt;/h2&gt;
&lt;p&gt;MSpec Behaviours are a way to group together a set of tests that 
    you want to re-use with different contexts. To do this you create a 
    separate class, add a Behaviors attribute to it, and move your 
    tests into that class. This behaviour class is then referenced by 
    using the  &lt;b&gt;Behaves_like&lt;/b&gt; syntax. An example taken from the 
    &lt;a href="http://community.sharpdevelop.net/blogs/mattward/articles/Machine.Specifications.Example.aspx"&gt;MSpec examples&lt;/a&gt; is shown below.&lt;/p&gt;
&lt;pre&gt;using System; 
using Machine.Specifications; 
 
namespace Banking.Tests 
{ 
    [Subject(&amp;quot;Date time parsing&amp;quot;)] 
    public class when_a_date_is_parsed_with_the_regular_expression_parser 
     : DateTimeParsingSpecs 
    { 
        Establish context = () =&amp;gt;  
         Parser = new RegexParser(); 
 
        Because of = () =&amp;gt;  
         ParsedDate = Parser.Parse(&amp;quot;2009/01/21&amp;quot;); 
 
        Behaves_like&amp;lt;DateTimeParsingBehavior&amp;gt; a_date_time_parser; 
    } 
 
    [Subject(&amp;quot;Date time parsing&amp;quot;)] 
    public class when_a_date_is_parsed_by_the_infrastructure 
     : DateTimeParsingSpecs 
    { 
        Establish context = () =&amp;gt;  
         Parser = new InfrastructureParser(); 
 
        Because of = () =&amp;gt;  
         ParsedDate = Parser.Parse(&amp;quot;2009/01/21&amp;quot;); 
 
        Behaves_like&amp;lt;DateTimeParsingBehavior&amp;gt; a_date_time_parser; 
    } 
 
    public abstract class DateTimeParsingSpecs 
    { 
        protected static DateTime ParsedDate; 
        protected static IParser Parser; 
    } 
 
    [Behaviors] 
    public class DateTimeParsingBehavior 
    { 
        protected static DateTime ParsedDate; 
 
        It should_parse_the_expected_date = () =&amp;gt;  
            ParsedDate.ShouldEqual(new DateTime(2009, 1, 21)); 
    } 
}&lt;/pre&gt;
&lt;p&gt;Here the behaviour of the parser returning a particular date has 
    been extracted and re-used across two different contexts.&lt;/p&gt;
&lt;p&gt;SharpDevelop is aware of MSpec behaviours and will display the 
    underlying tests. The date time parser specifications above will be 
    displayed in the Unit Tests window as shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="MSpec behaviours in Unit Tests window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecBehavioursInUnitTestsWindow.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;For further details on MSpec behaviours take a look at the 
    &lt;a href="http://lostechies.com/jamesgregory/2010/01/18/behaviours-in-mspec/"&gt;Behaviours in MSpec&lt;/a&gt; post by James Gregory.&lt;/p&gt;
&lt;h2&gt;Further MSpec Information&lt;/h2&gt;
&lt;p&gt;Here is a selection of links to further information on MSpec 
    that you may find useful.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://community.sharpdevelop.net/blogs/mattward/articles/machine.aspx"&gt;MSpec on Github&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://codebetter.com/aaronjensen/2008/05/08/introducing-machine-specifications-or-mspec-for-short/"&gt;Introducing MSpec&lt;/a&gt; - Aaron Jensen&amp;#39;s post that first 
    introduced MSpec.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://elegantcode.com/2010/02/19/getting-started-with-machine-specifications-mspec/"&gt;Getting Started with MSpec&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://wekeroad.com/mvc-storefront/kona-3/"&gt;Learning 
    BDD&lt;/a&gt; - If you prefer to learn by watching a video check out Rob 
    Conery&amp;#39;s BDD screencast where he discusses BDD and also covers 
    MSpec.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://wekeroad.com/blog/make-bdd-your-bff-2/"&gt;Make BDD 
    your BFF&lt;/a&gt; - Rob Conery walks through BDD with MSpec.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.awkwardcoder.com/index.php/2010/10/11/teaching-a-string-calculator-how-to-behave/"&gt;Teaching a String Calculator How to Behave&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://lostechies.com/jamesgregory/2010/01/18/behaviours-in-mspec/"&gt;Behaviours in MSpec&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://community.sharpdevelop.net/aggbug.aspx?PostID=37615" width="1" height="1"&gt;</description><category domain="http://community.sharpdevelop.net/blogs/mattward/archive/tags/MSpec/default.aspx">MSpec</category></item></channel></rss>