<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Christofer Löf</title>
  <generator uri="http://effectif.com/nesta">Nesta</generator>
  <id>tag:christoferlof.com,2009:/</id>
  
  <link rel="alternate" href="http://christoferlof.com" />
  <subtitle type="text">Thoughts on software development</subtitle>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/christoferlof" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="christoferlof" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title>Portable Class Library and ICommand</title>
    <link type="text/html" rel="alternate" href="http://christoferlof.com/testing/portable-class-library-icommand" />
    <id>tag:christoferlof.com,2012-03-13:/testing/portable-class-library-icommand</id>
    <content type="html">&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_8 suffix_4'&gt;
  &lt;h2&gt;Unit testing&lt;/h2&gt;
  &lt;p&gt;A while ago &lt;a href="windows-phone-unit-testing"&gt;I wrote an article&lt;/a&gt; describing how the Portable Class Library can enable us to write Unit Tests for our Windows Phone apps using the tools and frameworks we&amp;#8217;re already familiar with. By adding an abstraction layer and using types common to the Base Class Library, &lt;a href="http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981"&gt;Portable Class Library&lt;/a&gt; and Windows Phone we can write unit tests using tools like MSTest and Moq.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_8'&gt;
  &lt;h2&gt;View Models &amp;amp; ICommand&lt;/h2&gt;
  &lt;p&gt;A popular approach for writing Windows Phone apps is to use the Model-View-ViewModel pattern and by that expose operations on the View Model as commands which can be data bound to buttons on the view. The interface which enables commands, &lt;code&gt;ICommand&lt;/code&gt;, isn&amp;#8217;t available in the Portable Class Library thus exposing that kind of commands from our &lt;span class="caps"&gt;PCL&lt;/span&gt; View Models isn&amp;#8217;t possible. In my earlier article I demonstrated how we can return concrete instances using factories but that requires a common known interface or base class. In the case of commands &amp;#8211; &lt;code&gt;ICommand&lt;/code&gt; is supposed to be that common interface &amp;#8211; but since it&amp;#8217;s not available in the &lt;span class="caps"&gt;PCL&lt;/span&gt; we need another abstraction.  By exposing the command as &lt;code&gt;Object&lt;/code&gt; on the View Model would enable the same approach as previously shown using Factories &amp;#8211; but hey &amp;#8211; how fun would that be? Not much.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_4 infobox' style='background-position: 5px 100px;'&gt;
  &lt;div class='infoboxcontent' style='margin-top: 100px;'&gt;
    &lt;h3&gt;Download&lt;/h3&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/portable-class-library-icommand/PhoneApp2.zip"&gt;Sample solution&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_8 suffix_4'&gt;
  &lt;h2&gt;BindingConverter to the rescue&lt;/h2&gt;
  &lt;p&gt;Instead we can implement a BindingConverter which allow us to dispatch the invocation of a command to a method on the View Model. So far this approach seems to work really well and when giving this solution a second thought I really believe it improved the architecture by separating the concerns even further &amp;#8211; the view specific &lt;code&gt;ICommand&lt;/code&gt; is no longer part of the model library.&lt;/p&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="nt"&gt;&amp;lt;Button&lt;/span&gt; &amp;#x000A;   &lt;span class="na"&gt;Content=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Button text&amp;quot;&lt;/span&gt; &amp;#x000A;   &lt;span class="na"&gt;Command=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;{ Binding &lt;/span&gt;&amp;#x000A;&lt;span class="s"&gt;      Converter={ StaticResource CommandConverter }, &lt;/span&gt;&amp;#x000A;&lt;span class="s"&gt;      ConverterParameter=&amp;#39;ViewModelMethodName&amp;#39; }&amp;quot;&lt;/span&gt; &amp;#x000A;   &lt;span class="na"&gt;CommandParameter=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;MethodParameter&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/portable-class-library-icommand/Button.xml' class='file'&gt;Button.xml&lt;/a&gt;&lt;/p&gt;
  &lt;p&gt;Thanks to &lt;a href="http://www.coderox.se"&gt;Johan Lindfors&lt;/a&gt; who initiated this discussion.&lt;/p&gt;
  &lt;h2&gt;Visual Studio &amp;#8220;11&amp;#8221;&lt;/h2&gt;
  &lt;p&gt;&lt;a href="https://twitter.com/davkean/statuses/179783353560600576"&gt;Today I learnt&lt;/a&gt; that Visual Studio &amp;#8220;11&amp;#8221; Beta has support for &lt;code&gt;ICommand&lt;/code&gt; when targeting Windows Phone, SilverLight, Metro and .&lt;span class="caps"&gt;NET&lt;/span&gt; 4.5. Pure awesomeness.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/christoferlof/~4/rSchssgOYB4" height="1" width="1"/&gt;</content>
    <published>2012-03-13T20:48:00+01:00</published>
  </entry>
  <entry>
    <title>Unit testing Windows Phone 7 apps</title>
    <link type="text/html" rel="alternate" href="http://christoferlof.com/testing/windows-phone-unit-testing" />
    <id>tag:christoferlof.com,2011-08-14:/testing/windows-phone-unit-testing</id>
    <content type="html">&lt;div class='grid_12 summary'&gt;
  The Portable Library Tool  is the key ingredient in enabling Visual Studio integrated unit testing of Windows Phone 7 apps.  This article will demonstrate how to get started and a technique in managing the required dependencies between the projects.
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6 suffix_6'&gt;
  &lt;h2&gt;What&amp;#8217;s this all about?&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Unit testing is in itself a big topic with lots of opinions on how and reasons for why and why not.  I will not dig into the why part further than stating &amp;#8211; I do it on my WP7 apps to ensure that I didn&amp;#8217;t break anything.  Until now I used to use &lt;a href="wp7-testing-extravaganza"&gt;my own testing framework&lt;/a&gt; &amp;#8211; which is now, thanks to the introduction of the Portable Library Tools, kind of obsolete.&lt;/p&gt;
  &lt;p&gt;&lt;a href="http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981"&gt;Portable Library Tools&lt;/a&gt; is a new Visual Studio add-in from Microsoft which enables you to create C# and Visual Basic libraries that run on a variety of .&lt;span class="caps"&gt;NET&lt;/span&gt;-based platforms without recompilation, including Windows Phone.  This enables us to create a Visual Studio project containing the phone agnostic code and reference it from both a phone project and a test project. But since the portable library doesn&amp;#8217;t know about phone specifics we may need to make them available to the library at runtime through patterns like &lt;em&gt;Dependency Injection&lt;/em&gt; , &lt;em&gt;Service Locator&lt;/em&gt; and &lt;em&gt;Factory&lt;/em&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6 infobox' style='background-position: 5px 330px;'&gt;
  &lt;blockquote class="quote"&gt;
  &lt;p class="quote"&gt;I can finally throw away my home brewed unit test fx!&lt;br /&gt;
  &lt;cite&gt;&amp;#8212; Christofer Löf&lt;/cite&gt;&lt;/p&gt;
  &lt;/blockquote&gt;
  &lt;div class='infoboxcontent' style='margin-top: 160px;'&gt;
    &lt;h3&gt;Read more about patterns&lt;/h3&gt;
    &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd458879.aspx"&gt;Dependency Injection&lt;/a&gt;&lt;br /&gt;
    &lt;a href="http://msdn.microsoft.com/en-us/library/ff648476.aspx"&gt;Service Locator&lt;/a&gt;&lt;br /&gt;
    &lt;a href="http://msdn.microsoft.com/en-us/library/ee817667.aspx"&gt;Factory&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;h2&gt;Getting started&lt;/h2&gt;
  &lt;p&gt;In order to get started &amp;#8211; the first thing would be to make sure you&amp;#8217;ve the &lt;a href="http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981"&gt;Portable Library Tools&lt;/a&gt; installed. They&amp;#8217;re easy to find and install through the Extensions Manager in Visual Studio. When installed you should have a new project type called &lt;em&gt;Portable Class Library&lt;/em&gt; available in the new project wizard.  Then you&amp;#8217;ll need three projects in your solution; The phone app-, the portable library- and the test- project. The phone app project and the test project should both have a reference to the portable library project.&lt;/p&gt;
  &lt;p&gt;In my first example I&amp;#8217;m writing a very simple test just to make sure that the project setup works as intended &amp;#8211; a calculator with an Add method. The calculator class is created within the library project and an unit test that simply invokes the Add method is created within the test project. Also I invoke the Add method from the phone app just to create a baseline knowing that the project setup is OK.&lt;/p&gt;
  &lt;p&gt;Green lights!&lt;/p&gt;
  &lt;h2&gt;Model-View-ViewModel-what?&lt;/h2&gt;
  &lt;p&gt;Two of the drivers behind the Model-View-ViewModel (&lt;span class="caps"&gt;MVVM&lt;/span&gt;) pattern are testability and code reuse.  By extracting the ViewModels from the phone project into the portable library project you will be able to test them and reuse them with ease.&lt;/p&gt;
  &lt;p&gt;For this next example I create a phone app that&amp;#8217;s implementing the &lt;span class="caps"&gt;MVVM&lt;/span&gt; pattern by choosing the &lt;em&gt;Databound Application&lt;/em&gt; template in the new project wizard. The template will create two simple view-models and tie them to their views.  The next thing would be for me to move the view-models to the library project.&lt;/p&gt;
  &lt;p&gt;So far this portable library journey has been friction free.  When moving the just created view-models to the library project the first perceived problem arise &amp;#8211; Visual Studio complains about the type &lt;a href="http://msdn.microsoft.com/en-us/library/ms668604(v=VS.95).aspx"&gt;&lt;code&gt;ObservableCollection&lt;/code&gt;&lt;/a&gt;.  That&amp;#8217;s because &lt;code&gt;ObservableCollection&lt;/code&gt; live in the &lt;code&gt;System.Windows&lt;/code&gt; assembly which isn&amp;#8217;t referenced by the portable library project. If I remove the .&lt;span class="caps"&gt;NET&lt;/span&gt; Fx 4 target from the portable library project &amp;#8211; I would be able to reference &lt;code&gt;System.Windows&lt;/code&gt; but I wouldn&amp;#8217;t be able to reference the portable library project from the test project since the portable library project then would be a SilverLight only library.&lt;/p&gt;
  &lt;p&gt;I&amp;#8217;m not changing the target setting because in this case I want to unit test my app. I wrote &lt;em&gt;perceived&lt;/em&gt; above just to state that this really isn&amp;#8217;t a big problem. The principle to solve the ObservableCollection challenge is the same as it would&amp;#8217;ve been for the phone&amp;#8217;s &lt;code&gt;NavigationService&lt;/code&gt; class for instance. There&amp;#8217;re things in the phone part of the  framework that you probably need to consume in the view-models.  The best way to gain access to phone specifics, like &lt;code&gt;ObservableCollection&lt;/code&gt; and &lt;code&gt;NavigationService&lt;/code&gt;, would be to fulfill the &lt;em&gt;dependency inversion principle&lt;/em&gt;.&lt;/p&gt;
  &lt;p&gt;In order to create an abstraction layer I need to change the type of the members that are returning &lt;code&gt;ObservableCollection&lt;/code&gt; to a type that&amp;#8217;s known by all my projects and exposes the functionality needed. That type would be &lt;code&gt;IList&amp;lt;T&amp;gt;&lt;/code&gt; in this case. The next thing I need is something that can provide me with an instance of &lt;code&gt;ObservableCollection&lt;/code&gt; at runtime &amp;#8211; since I can&amp;#8217;t instantiate it in the view-model. Furthermore I need an instance of something that implements the above defined contract during test execution. This is the part where more choices and opinions about how to provide instances at runtime arise. There&amp;#8217;re a couple of libraries and guidance out there which could prescribe how and aid me in wiring up the dependency injection. But for the simplicity of this example I chose not to bring in an external framework like that.  A simple &lt;em&gt;Factory&lt;/em&gt; will solve this for now.&lt;/p&gt;
  &lt;p&gt;The base implementation of the &lt;code&gt;ListFactory&lt;/code&gt; will return instances of &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; , which satisfies the above contract and is available on all targeted platforms. All I need to do is to make sure that the factory is instantiated before the tests run. In the phone project I add a new factory class which derives from the ListFactory base class and overrides the &lt;code&gt;CreateList&lt;/code&gt; method in order to return an instance of &lt;code&gt;ObservableCollection&lt;/code&gt; instead. The &lt;code&gt;ObservableListFactory&lt;/code&gt; is instantiated once in my phone project&amp;#8217;s &lt;code&gt;App&lt;/code&gt; class and it will hold a reference to itself thru the &lt;code&gt;Current&lt;/code&gt; property. With this approach I will be provided a list implementation that&amp;#8217;s compatible with the current execution context.&lt;/p&gt;
  &lt;h2&gt;Wrapping it up&lt;/h2&gt;
  &lt;p&gt;Splitting up your Windows Phone app in two projects &amp;#8211; the phone app project and a portable library project &amp;#8211; should be your pattern of choice if you want to unit test your implementation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6 infobox' style='background-position: 5px 1480px;'&gt;
  &lt;div class='clipimg'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/portable-class-library.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/portable-class-library.png" style="margin-left: -230px; margin-top: -200px;" title="Portable class library" alt="Portable class library" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='clipimg'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/references.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/references.png" style="margin-left: 10px; margin-top: -86px;" title="References" alt="References" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='clipimg'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/test-results.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/test-results.png" style="margin-left: -22px; margin-top: -4px;" title="Test results in Visual Studio" alt="Test results in Visual Studio" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='clipimg'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/move-viewmodels.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/move-viewmodels.png" style="margin-left: -8px; margin-top: -240px;" title="Move view-models to library project" alt="Move view-models to library project" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='clipimg'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/no-observablecollection.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/no-observablecollection.png" style="margin-left: -280px; margin-top: -260px;" title="ObservableCollection isn&amp;#39;t available" alt="ObservableCollection isn&amp;#39;t available" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='infoboxcontent' style='margin-top: 150px;'&gt;
    &lt;h3&gt;Libraries and guidance for &lt;span class="caps"&gt;MVVM&lt;/span&gt;&lt;/h3&gt;
    &lt;p&gt;&lt;a href="http://caliburnmicro.codeplex.com/"&gt;Caliburn Micro&lt;/a&gt;&lt;br /&gt;
    &lt;a href="http://mvvmlight.codeplex.com/"&gt;&lt;span class="caps"&gt;MVVM&lt;/span&gt; Light&lt;/a&gt;&lt;br /&gt;
    &lt;a href="http://msdn.microsoft.com/en-us/library/gg405484(v=PandP.40).aspx"&gt;Prism&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='clipimg' style='margin-top: 150px;'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/list-factory.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/list-factory.png" style="margin-left: -140px; margin-top: -140px;" title="The Factory" alt="The Factory" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class='clipimg'&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/list-factory-usage.png"&gt;&lt;img src="/attachments/windows-phone-unit-testing/list-factory-usage.png" style="margin-left: -280px; margin-top: -140px;" title="Using the Factory" alt="Using the Factory" /&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Is &lt;span class="caps"&gt;MVVM&lt;/span&gt; and unit testing suitable for all WP7 projects?&lt;br /&gt;
  It certainly adds complexity to your application implementation but one great benefit you&amp;#8217;ll get from it is confidence &amp;#8211; knowing that you didn&amp;#8217;t break anything from release 1 to release 2. The path I usually take is to first create some kind of &lt;em&gt;Proof of Concept&lt;/em&gt; of the app in mind in order to validate the overall idea and to run a few iterations on the end user experience. When I believe in the overall I start to refactor my way into the patterns and practices described in this article.&lt;br /&gt;
  In the end it&amp;#8217;s the end-user experience that matters.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6 infobox' style='background-position: 5px 14px;'&gt;
  &lt;div class='infoboxcontent'&gt;
    &lt;h3&gt;Download&lt;/h3&gt;
    &lt;p&gt;&lt;a href="http://christoferlof.com/attachments/windows-phone-unit-testing/TestablePhoneApp.zip"&gt;Sample solution&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;script type='text/javascript'&gt;
  //&lt;![CDATA[
    $(".clipimg a").fancybox();
  //]]&gt;
&lt;/script&gt;
&lt;img src="http://feeds.feedburner.com/~r/christoferlof/~4/SM1UlLIDLUA" height="1" width="1"/&gt;</content>
    <published>2011-08-14T20:50:00+01:00</published>
  </entry>
  <entry>
    <title>Driver's log for Windows Phone 7</title>
    <link type="text/html" rel="alternate" href="http://christoferlof.com/windowsphone/driverslog" />
    <id>tag:christoferlof.com,2011-03-12:/windowsphone/driverslog</id>
    <content type="html">&lt;div class='grid_12 summary'&gt;
  Driver's log is a simple application for Windows Phone 7 which helps you keep track of mileage and expenses while on the road.
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_9'&gt;
  &lt;p&gt;As a consultant I travel quite a lot by car. By the end of every month I need to fill in our corporate Excel spreadsheet with my mileage and send it to payroll in order to get reimbursed.&lt;/p&gt;
  &lt;p&gt;When I received by &lt;span class="caps"&gt;HTC&lt;/span&gt; Mozart a couple of weeks ago I immediately felt the urge to develop for it and get to know the platform. So I thought for myself &amp;#8211; why not smash two birds with the same stone and learn something while I build something that I need?&lt;/p&gt;
  &lt;p&gt;Said and done &amp;#8211; I named the application &amp;#8220;Driver&amp;#8217;s log&amp;#8221;. It allows me (and you, since it&amp;#8217;s available on the Marketplace) to keep track of my travels and car mileage and associated expenses like gas and parking. By the end of the month I export my log through e-mail and then easily copies the entries from the e-mail into the form my manager expects. (&amp;#8220;Convince manager to accept Driver&amp;#8217;s log export format&amp;#8221; is on my to-do list). This saves me some time but more important &amp;#8211;  since it&amp;#8217;s fast and easy &amp;#8211; it ensures that I really report all trips and by the end of the day it means that I get accurately reimbursed (money saved).&lt;/p&gt;
  &lt;p&gt;So how did I implement Driver&amp;#8217;s log?&lt;br /&gt;
  On the framework side I took a dependency on &lt;a href="http://caliburnmicro.codeplex.com/"&gt;Caliburn.Micro&lt;/a&gt; to do proper unit testing with &lt;a href="https://github.com/christoferlof/VFx"&gt;VFx&lt;/a&gt;. Caliburn brings &amp;#8211; among other things &amp;#8211; Windows Phone &lt;span class="caps"&gt;API&lt;/span&gt; abstractions, Dependency Injection Container and Messaging to the table. This allowed me to clearly separate the Phone UI and Phone specific services from the core of the Driver&amp;#8217;s log application &amp;#8211; a necessity for unit testing the model outside of the emulator.&lt;br /&gt;
  I also took a dependency on the &lt;a href="http://silverlight.codeplex.com/"&gt;Silverlight Toolkit&lt;/a&gt;. It provides Driver&amp;#8217;s log with nice date pickers and touch effects.&lt;/p&gt;
  &lt;p&gt;At the time of writing this Driver&amp;#8217;s log is at version 1.2. I have a few more things on my list that I want to implement. Besides from them I want to hear from you. Please post your questions, concerns, requests, criticism, praise and ideas in the comments section below.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_3'&gt;
  &lt;p&gt;&lt;img src="/attachments/driverslog/driverslog1-small.png" id="driverslogscreenshots" title="Screenshots" alt="Screenshots" /&gt;&lt;/p&gt;
  &lt;p&gt;&lt;a href="http://social.zune.net/redirect?type=phoneApp&amp;amp;id=804f52f1-9334-e011-854c-00237de2db9e&amp;amp;source=blog"&gt;&lt;img src="/attachments/driverslog/marketplace.png" title="Download from Marketplace" alt="Download from Marketplace" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;script type='text/javascript'&gt;
  //&lt;![CDATA[
    $("#driverslogscreenshots").click(function(){
    	$.fancybox([
    		'/attachments/driverslog/driverslog1.png',
    		'/attachments/driverslog/driverslog2.png',
    		'/attachments/driverslog/driverslog3.png',
    		'/attachments/driverslog/driverslog4.png'
    	],{
    		'type'			: 'image',
    		'titlePosition': 'over',
    		'titleFormat'  : function(title, currentArray, currentIndex, currentOpts) {
    			return '&lt;span id="fancybox-title-over"&gt;Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '&lt;/span&gt;';
    		}
    	});
    });
  //]]&gt;
&lt;/script&gt;
&lt;img src="http://feeds.feedburner.com/~r/christoferlof/~4/1jc3KdCSnRg" height="1" width="1"/&gt;</content>
    <published>2011-03-12T09:05:00+01:00</published>
  </entry>
  <entry>
    <title>WP7 Testing Extravaganza!</title>
    <link type="text/html" rel="alternate" href="http://christoferlof.com/testing/wp7-testing-extravaganza" />
    <id>tag:christoferlof.com,2010-11-06:/testing/wp7-testing-extravaganza</id>
    <content type="html">&lt;div class='grid_12 summary'&gt;
  Writing an Unit and Integration Test framework for Windows Phone 7 Silverlight isn't hard once you got hold of all moving parts. The Victoria Framework helps me to successfully Unit Test my Windows Phone 7 applications without deploying to the emulator.
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;During my paternity leave this summer I had some moments which I could spend on coding-for-fun. Like a whole lot of others these days I began to write a couple of Windows Phone 7 apps. Among the first things I looked into was the availability of testing frameworks especially suited for Windows Phone developement. Back then I found one &amp;#8211; the silverlight toolkit unit testing framework. It&amp;#8217;s an impressive piece of work. But in my humble opinion it lacks, currently, two major features. The first feature is; execute unit test outside of the emulator or the device i.e. on your computer.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;For a &lt;span class="caps"&gt;TDD&lt;/span&gt; style of development having to deploy to the emulator to execute one test just eats too much of my precious time. I&amp;#8217;ve understood that the APIs of the Silverlight Unit Test Framework are compatible with the VSTest APIs so linking files between projects would probably be one way to execute the tests on the development machine. But that would force me to create shadow projects to link files and execute the tests outside from the Silverlight runtime &amp;#8211; I don&amp;#8217;t want that. The second feature I believe the Silverlight Testing Framework is lacking, which I really thought was there, is support for UI tests.&lt;/p&gt;
  &lt;p&gt;So. When you&amp;#8217;re a guy who likes to write frameworks and has some spare time. What do you do? &lt;strong&gt;You roll your own testing framework of course!&lt;/strong&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;h2&gt;Unit Tests&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;The amount of code that is required to execute tests within the Silverlight runtime from a console app isn&amp;#8217;t too many. The number of moving parts within the solution are a few more though. The console app hosts a Browser control. The source &lt;span class="caps"&gt;URL&lt;/span&gt; of the &lt;code&gt;WebBrowser&lt;/code&gt; control is set to a static &lt;span class="caps"&gt;HTML&lt;/span&gt; page (the &lt;code&gt;RunnerPage&lt;/code&gt;) which hosts the generated xap file. The console app calls a JavaScript function in the &lt;span class="caps"&gt;HTML&lt;/span&gt; page using the &lt;code&gt;InvokeScript&lt;/code&gt; method of the Browser control. The JavaScript in the &lt;span class="caps"&gt;HTML&lt;/span&gt; page executes the &lt;code&gt;TestRunner&lt;/code&gt; within the xap package by calling a method exposed by the Silverlight JavaScript Bridge. The key parts here are the two different JavaScript bridges exposed by the &lt;code&gt;WebBrowser&lt;/code&gt; control and by Silverlight.&lt;/p&gt;
  &lt;p&gt;The console app creates the xap package on the fly by examining the &lt;em&gt;Package&lt;/em&gt; folder and include all assemblies found in there. The resulting xap is copied to the &lt;em&gt;Content&lt;/em&gt; folder, where the RunnerPage expects it to be.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;&lt;img src="/attachments/wp7-testing-extravaganza/layers.png" alt="" /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Writing the Unit Tests is as simple as you might expect. Create a Windows Phone 7 class library, add your tests and mark them with the Fact attribute.&lt;/p&gt;
  &lt;p&gt;To run the tests you have to copy the test assembly to the Package folder as previously described. To do that you could set up post build events in Visual Studio or just write a simple script to handle the copying and executing of tests for you. I&amp;#8217;m currently using my new best friend &lt;a href="https://github.com/JamesKovacs/psake"&gt;psake&lt;/a&gt; to handle all that for me. It allows me to just write &lt;code&gt;.\test.ps1&lt;/code&gt; in a PowerShell in order to build and unit test my apps.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="na"&gt;[Fact]&lt;/span&gt;&amp;#x000A;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;should_set_id_on_save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;    &amp;#x000A;    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;TodoItem&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;    &amp;#x000A;    &lt;span class="n"&gt;TodoItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;    &lt;span class="n"&gt;TodoItem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SaveChanges&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;&amp;#x000A;    &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;!=&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/wp7-testing-extravaganza/TodoItemTests.cs' class='file'&gt;TodoItemTests.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;h2&gt;UI Tests&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="na"&gt;[Fact]&lt;/span&gt;&amp;#x000A;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;should_create_todo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;    &lt;span class="n"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/CreatePage.xaml&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;        &lt;span class="n"&gt;p&lt;/span&gt;   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TextBox&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;TitleTextBox&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&amp;#x000A;            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;New Todo&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;        &lt;span class="n"&gt;p&lt;/span&gt;   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;CreateButton&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&amp;#x000A;            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Click&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;        &lt;span class="n"&gt;p&lt;/span&gt;   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TextBox&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;IdTextBox&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&amp;#x000A;            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WaitForText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &amp;#x000A;&amp;#x000A;        &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;!=&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&amp;#x000A;    &lt;span class="p"&gt;});&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/wp7-testing-extravaganza/TodoTests.cs' class='file'&gt;TodoTests.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;The UI test (or Integration tests as I call them in the Framework) functionality is at this point very immature. My goal here is to run the UI using the Silverlight Automation APIs. For the basic controls like Buttons and TextBoxes this isn&amp;#8217;t a problem. But for the ApplicationBar, for instance, it&amp;#8217;s harder because that control isn&amp;#8217;t a Silverlight Control and hence it lacks automation support. I believe the UI test support in the framework will be a mix of Silverlight Automation and a pinch of reflection magic by relying on some naming conventions. (It&amp;#8217;s true &amp;#8211; it really will be magic. Promise.)&lt;/p&gt;
  &lt;p&gt;Lately I&amp;#8217;ve used the &lt;a href="http://aspnet.codeplex.com/wikipage?title=ASP.NET%20QA&amp;amp;referringTitle=Home"&gt;Lightweight Test Automation Framework&lt;/a&gt; by the &lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt; QA team in a bunch of projects and I really like the APIs it provides for writing tests. So it&amp;#8217;s not a coincidence that the VFx integration test APIs reminds you of &lt;span class="caps"&gt;LTAF&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#if INTEGRATIONTEST&lt;/span&gt;&amp;#x000A;    &lt;span class="n"&gt;RunIntegrationTests&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;&lt;span class="cp"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/wp7-testing-extravaganza/App.xaml.cs' class='file'&gt;App.xaml.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;The UI tests are run with the same runner as the unit tests. Currently the tests have to live in the app they are testing and the runner is executed by defining the &lt;span class="caps"&gt;INTEGRATIONTEST&lt;/span&gt; compilation symbol. This is clearly something I want to change in the future.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;h2&gt;Download&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6 suffix_6'&gt;
  &lt;p&gt;The framework, which I call the &lt;em&gt;Victoria Framework&lt;/em&gt; or just &lt;em&gt;VFx&lt;/em&gt;, is hosted at &lt;a href="https://github.com/christoferlof/VFx"&gt;github&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/christoferlof/~4/-DDMcnhUYJQ" height="1" width="1"/&gt;</content>
    <published>2010-11-06T16:40:00+01:00</published>
  </entry>
  <entry>
    <title>Context/Specification enhetstester</title>
    <link type="text/html" rel="alternate" href="http://christoferlof.com/testing/context-specification-tests" />
    <id>tag:christoferlof.com,2010-09-05:/testing/context-specification-tests</id>
    <content type="html">&lt;div class='grid_12 summary'&gt;
  Context/Specification är en alternativ stil att skriva enhetstester enligt. Stilen kan vara ett verktyg i arbetet med att reda ut en komplex flora av enhetstester, samtidigt som den kan ge en tydligare koppling till kravställningen.
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Majoriteten av alla enhetstester skrivs sannolikt enligt den så kallade &lt;em&gt;Arrange-Act-Assert&lt;/em&gt; stilen. Stilen går ut på att först etablera scenen för vad som skall utspela sig ( &lt;em&gt;Arrange&lt;/em&gt; ), därefter utförs handlingen ( &lt;em&gt;Act&lt;/em&gt; ), för att till sist försäkra sig om att det gick som man hade föreställt sig ( &lt;em&gt;Assert&lt;/em&gt; ). Detta sker vanligen inom ramen för en testmetod. Att hålla allt som rör ett testfall inom en testmetod kan initialt ses som klart fördelaktigt då man anser att det blir enkelt och läsbart.&lt;/p&gt;
  &lt;p&gt;I takt med att antalet tester ökar ökar också kraven på att testerna är minst lika underhållsvänliga och läsbara som den kod de avser att testa. Nu och då träffar jag utvecklare som anser enhetstester bidrar till fler problem istället för att avhjälpa eller förhindra dem.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ServiceReturnsFooTest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;           &amp;#x000A;   &lt;span class="c1"&gt;//arrange&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FooService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;   &lt;span class="c1"&gt;//act&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;   &lt;span class="c1"&gt;//assert&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Foo&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/context-specification-tests/basic-arrange-act-assert.cs' class='file'&gt;basic-arrange-act-assert.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;blockquote class="quote"&gt;
  &lt;p class="quote"&gt;Testerna går sönder för lätt när systemet under test förändras. När vi skall reparera testerna vet ingen riktigt vad de faktiskt testar &amp;#8211; de är för komplexa. &lt;br /&gt;
    Eftersom vi inte riktigt förstår testerna så litar vi inte på dem. Oftast är allt grönt. Ibland är några tester röda utan någon känd anledning. &lt;br /&gt;
    Skall vi lyckas med enhetstester känns det som vi behöver tester som testar testerna.&lt;br /&gt;
    &lt;cite&gt;&amp;#8212; Frustrerad utvecklare&lt;/cite&gt;&lt;/p&gt;
  &lt;/blockquote&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Att testerna går sönder lätt kan bero på flera saker. Ett vanligt problem är att testerna är överspecificerade och använder mockramverk allt för intensivt för att simulera interna beroenden. När testerna väl går sönder, eller behöver justeras av andra anledningar, så gäller det som alltid att få ställen har påverkats och därmed behöver ändras. Det handlar helt enkelt om att minimera duplicerad kod i testerna. Det är inte alls ovanligt att två tester har snarlika eller rent av samma arrange och act block för att sedan bara skilja sig åt i assert blocket.&lt;/p&gt;
  &lt;p&gt;För att snabbt förstå vad ett test verifierar så bör namnet på testet vara tydligt samt att antalet asserts hålls så nära ett som möjligt. Detta för att slippa titta i testet för att möjligen förstå vad det faktiskt avser att testa. Ett &lt;code&gt;BazTest&lt;/code&gt; med tre asserts är ett exempel på ett otydligt testfall.&lt;/p&gt;
  &lt;p&gt;I många fall vill man försäkra sig om att flera förändringar har skett i tillståndet av systemet under test och då krävs flera assert uttryck. Så snart fler asserts introduceras till en testmetod blir frågan snart vad det är som faktiskt försäkras och testas? För att tydliggöra det så ger man möjligen testmetoden ett långt fint namn eller kryddar på med kommentarer i testmetoden. Men vad man då fortfarande går miste om är den visualisering som resultatet av en testsvit faktiskt kan ge. Om en assert av fyra i ett testfall inte går igenom &amp;#8211; vad innebär egentligen det? Vad är det som inte fungerar? Vissa saker fungerar uppenbarligen eftersom tre asserts accepterades. Borde inte resultatet bli halvgrönt åtminstone? Frågorna kan bli många. Kanske måste man till och med debugga testfallet för att verkligen få reda på vad som inte fungerar som planerat.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="c1"&gt;//arrange for test 1&lt;/span&gt;&amp;#x000A;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IFooRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;few&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt;&amp;#x000A;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FooService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;&amp;#x000A;&lt;span class="c1"&gt;//arrange for test 2&lt;/span&gt;&amp;#x000A;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IFooRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt; &lt;span class="n"&gt;comes&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt; &lt;span class="n"&gt;again&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt;&amp;#x000A;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FooService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/context-specification-tests/dupe-arranges.cs' class='file'&gt;dupe-arranges.cs&lt;/a&gt;&lt;/p&gt;&lt;div class="source"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BazTest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;   &lt;span class="c1"&gt;//arrange&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FooService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;           &amp;#x000A;   &lt;span class="c1"&gt;//act&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Baz&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="c1"&gt;//we need some asserts.. right?&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &amp;#x000A;   &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsTrue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Succeeded&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;   &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;42&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TheMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/context-specification-tests/multiple-asserts.cs' class='file'&gt;multiple-asserts.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;h2&gt;Tydligare tester&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Context/Specification stilen kan hjälpa dig skriva stabilare och tydligare tester genom att på ett naturligt sätt bryta ut arrange och act blocken samt att hålla nere antal asserts till en per testmetod. Stilen innebär dock att varje arrange (i detta fall kallad sammanhang eller context) får en egen dedikerad klass vilket till en början kan uppfattas som överflödigt.&lt;/p&gt;
  &lt;p&gt;Genom att gruppera klasser i varandra och möjligheten till att ärva ett sammanhang från ett annat sammanhang gör att man snart ändrar uppfattning om att många klasser skulle vara ett problem.&lt;/p&gt;
  &lt;p&gt;För att anamma Context/Specification stilen behövs en basklass som testfallen ärver från. Basklassen, vilken förslagsvis kallas &lt;a href="http://christoferlof.com/attachments/context-specification-tests/ContextSpecification.cs"&gt;ContextSpecification&lt;/a&gt;, exponerar tre metoder som testfallet kan överrida och därmed nyttja.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Metoden &lt;code&gt;Context&lt;/code&gt; motsvarar arrange blocket &amp;#8211; det vill säga här etableras sammanhanget och scenen för systemet under test riggas upp. &lt;code&gt;Because&lt;/code&gt; innehåller troligen endast en rad kod &amp;#8211; den rad som förändrar tillståndet i systemet under test. Tänk varför förändras tillståndet? Jo &amp;#8211; därför&amp;#8230; så har du en förklaring till namnvalet. Båda dessa metoder anropas i basklassen i en metod som är märkt med attributet &lt;code&gt;TestInitialize&lt;/code&gt; (Varierar beroende på val av testramverk). Det innebär att metoderna &lt;code&gt;Context&lt;/code&gt; och &lt;code&gt;Because&lt;/code&gt; exekveras innan varje testmetod för att på så vis återställa sammanhanget och tillståndet till dess utgångspunkt för att sedan exekvera testet.&lt;/p&gt;
  &lt;p&gt;Varje testmetod innehåller endast ett &lt;code&gt;assert&lt;/code&gt;-uttryck som försäkrar att resultatet av tillståndsförändringen är vad som förväntas.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_8 prefix_2 suffix_2'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="na"&gt;[TestClass]&lt;/span&gt;&amp;#x000A;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;when_foo_is_invoked&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ContextSpecification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;FooService&lt;/span&gt;    &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;        &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;   &amp;#x000A;   &lt;span class="c1"&gt;//&amp;quot;arrange&amp;quot;&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IFooRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;&amp;#x000A;       &lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt;&amp;#x000A;       &lt;span class="n"&gt;Service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FooService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="c1"&gt;//&amp;quot;act&amp;quot;&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Because&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="n"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="c1"&gt;//&amp;quot;assert&amp;quot;&lt;/span&gt;&amp;#x000A;&lt;span class="na"&gt;   [TestMethod]&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;should_return_foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Foo&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/context-specification-tests/basic-context-spec.cs' class='file'&gt;basic-context-spec.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;h2&gt;Ökad återanvändbarhet&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Den riktiga styrkan med Context/Specification stilen är som tidigare nämnt dess enkelhet att ärvas och återanvändas.&lt;/p&gt;
  &lt;p&gt;En tjänst som skapar kontakter tar emot ett meddelande innehållandes kontaktens förnamn, efternamn och e-postadress. Samtliga dessa egenskaper är obligatoriska för att en ny kontakt skall få sparas ned i systemet. I det fall någon av de nämnda egenskaperna på kontakten inte uppfyller valideringsreglerna blir svaret på meddelandet &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;
  &lt;p&gt;För att testa alla kombinationer i detta scenario med arrange-act-assert stilen är det lätt hänt att duplicerad kod skapas genom tre testmetoder vilka innehåller snarlika arrange block samt identiska act och 	assert block.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='grid_6'&gt;
  &lt;p&gt;Med Context/Specification stilen skapas i detta fall en basklass för sammanhanget där alla kontaktens egenskaper initieras till ett godkänt värde. För varje egenskap på meddelandet skapas ytterligare en klass som ärver från grundsammanhanget och som endast justerar aktuell egenskap till ett otillåtet värde. På så vis kan en testsvit skapas med god täckningsgrad, god läsbarhet och god underhållbarhet. Notera hur ett arrange block (i form av &lt;code&gt;Context&lt;/code&gt;) och en &lt;code&gt;Assert&lt;/code&gt; resulterar i tre testfall (testmetoden är del av basklassen &lt;code&gt;when_validating_contact&lt;/code&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_8 prefix_2 suffix_2'&gt;
  &lt;div class="source"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;when_validating_contact&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ContextSpecification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;ContactService&lt;/span&gt;    &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;              &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;Contact&lt;/span&gt;           &lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;           &amp;#x000A;       &lt;span class="n"&gt;Service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ContactService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;       &lt;span class="c1"&gt;//initialize the contact with known and valid values&lt;/span&gt;&amp;#x000A;       &lt;span class="n"&gt;Contact&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Contact&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;           &lt;span class="n"&gt;Email&lt;/span&gt;       &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;e-mail&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&amp;#x000A;           &lt;span class="n"&gt;FirstName&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;fist-name&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&amp;#x000A;           &lt;span class="n"&gt;LastName&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;last-name&amp;quot;&lt;/span&gt;&amp;#x000A;       &lt;span class="p"&gt;};&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Because&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="n"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;&lt;span class="na"&gt;   [TestClass]&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;with_empty_email&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;when_validating_contact&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;           &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;           &lt;span class="c1"&gt;//set email to an invalid value&lt;/span&gt;&amp;#x000A;           &lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;        &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;&lt;span class="na"&gt;   [TestClass]&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;with_empty_firstname&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;when_validating_contact&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;           &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;           &lt;span class="c1"&gt;//set firstname to an invalid value&lt;/span&gt;&amp;#x000A;           &lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FirstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;       &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;&lt;span class="na"&gt;   [TestClass]&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;with_empty_lastname&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;when_validating_contact&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;           &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&amp;#x000A;           &lt;span class="c1"&gt;//set lastname to an invalid value&lt;/span&gt;&amp;#x000A;           &lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&amp;#x000A;       &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&amp;#x000A;   &lt;span class="c1"&gt;//note - this test method is part of the base class&lt;/span&gt;&amp;#x000A;&lt;span class="na"&gt;   [TestMethod]&lt;/span&gt;&amp;#x000A;   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;should_not_be_valid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&amp;#x000A;       &lt;span class="kt"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsFalse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&amp;#x000A;   &lt;span class="p"&gt;}&lt;/span&gt;&amp;#x000A;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
  &lt;p&gt;&lt;a href='http://christoferlof.com/attachments/context-specification-tests/adv-context-spec.cs' class='file'&gt;adv-context-spec.cs&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;h2&gt;Koppling till kravställning&lt;/h2&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;div class='grid_12'&gt;
  &lt;p&gt;Ordet &lt;em&gt;specification&lt;/em&gt; används istället för ordet &lt;em&gt;test&lt;/em&gt; för att skapa en tydligare upplevelse av ett det är exekverbara specifikationer som skapas. Själv ser jag en klar fördel med att åtminstone använda ett språk i namngivning av contexts och specifications som ligger så nära som möjligt det språk som används av kunden i dess kravställning och kommunikation. Man tenderar då få tester (specifikationer) som tydliggör varför klasser finns och hur deras beteende associerar till kundens önskemål. Att gruppera testfallen efter &lt;em&gt;feature&lt;/em&gt; eller &lt;em&gt;user&lt;/em&gt; &lt;em&gt;story&lt;/em&gt; kan stärka spårbarheten ytterligare.&lt;/p&gt;
  &lt;p&gt;&lt;img src="/attachments/context-specification-tests/tick.png" alt="" /&gt; &lt;code&gt;when_validating_contact+with_empty_name.should_not_be_valid&lt;/code&gt; är enligt min mening ett mer tydligt testresultat än &lt;img src="/attachments/context-specification-tests/tick.png" alt="" /&gt; &lt;code&gt;BazTest&lt;/code&gt;. Kanske förstår också din kund vad som faktiskt fungerar av det hon efterfrågar.&lt;/p&gt;
&lt;/div&gt;
&lt;div class='clear'&gt;&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/christoferlof/~4/kzq2O-029OM" height="1" width="1"/&gt;</content>
    <published>2010-09-05T21:46:00+01:00</published>
  </entry>
</feed><!-- page cached: 2012-14-03 22:17:28 -->

