<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ten Tonne Baby &#187; .NET</title>
	<atom:link href="http://www.tentonnebaby.com/category/development/dotnet-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tentonnebaby.com</link>
	<description>Discussion on Web Technologies, Design and London</description>
	<lastBuildDate>Thu, 19 Jan 2012 09:33:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Specflow to test a form post scenario with ASP.NET MVC 3</title>
		<link>http://www.tentonnebaby.com/2010/12/16/using-specflow-to-test-a-form-post-scenario-with-asp-net-mvc-3/</link>
		<comments>http://www.tentonnebaby.com/2010/12/16/using-specflow-to-test-a-form-post-scenario-with-asp-net-mvc-3/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 01:06:28 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[moq]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[specflow]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.tentonnebaby.com/2010/12/16/using-specflow-to-test-a-form-post-scenario-with-asp-net-mvc-3/</guid>
		<description><![CDATA[I was working on a feature recently that began with a scenario in Specflow, something like this…
Scenario: New password min length
    Given I have entered a password with only 5 characters
    When I hit save
    Then I see a validation error against password
So, first off I’m [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a feature recently that began with a scenario in Specflow, something like this…</p>
<pre class="brush: csharp;">Scenario: New password min length
    Given I have entered a password with only 5 characters
    When I hit save
    Then I see a validation error against password</pre>
<p>So, first off I’m going to need to simulate a value being posted to an action method in the form collection. The simplest approach would be something like this…</p>
<pre class="brush: csharp;">[HttpPost]
public ActionResult ResetPasswordSubmit(string password)
{
}</pre>
<p>However, it doesn’t really feel right to put the business rules around password validation directly in to this action method, as there may be other places in the application where a user can change their password. A better approach would seem to be implementing the validation rules as Data Annotation attributes against the model class. The method then becomes something like this…</p>
<pre class="brush: csharp;">public class SecurityController : Controller
{
   private readonly ISecurityService securityService;

   public SecurityController(ISecurityService service)
   {
      this.securityService = service;
   }

   [HttpPost]
   public ActionResult ResetPasswordSubmit()
   {
      var user = this.securityService.GetSignedInUser();

      if (this.TryUpdateModel(user, new string[] { &quot;Password&quot; }))
      {
         // save user
      }
   }
}</pre>
<p>Working with a model class that looks a bit like…</p>
<pre class="brush: csharp;">using System.ComponentModel.DataAnnotations;

public class User
{
    [StringLength(20, MinimumLength = 5)]
    public string Password { get; set; }
}</pre>
<p>So what would happen now is that the browser would post a single value in the form collection (Password). When TryUpdateModel is called, the framework will use a model binder to map the request information onto the model properties. The model binder to use can change according to the type being mapped, but in this case the DefaultModelBinder will be used.</p>
<p>The model binders need to make use of information from the request (such as posted form values, querystring etc). Value Providers provide an abstraction around this, and the framework ships with various providers to interrogate the different types of request information. In our example, the FormValueProvider would map the Password value from the form collection onto the Password property of our model class. As part of this process, the DefaultModelBinder will apply any validation rules set up via Data Annotation attributes, and add any validation errors into ModelState.</p>
<p>Time to get started with our unit test. With SpecFlow, the scenario is defined in a plain English language based on the Cucumber DSL. When you save the scenario, Specflow will generate a test fixture that includes a test method with the scenario name – in our case NewPasswordMinLength. The test method will attempt to call a method for each individual step in the scenario based on binding attributes applied to the methods.</p>
<p>We can start by adding a new item, and picking Specflow Step definition. To map the scenario steps in our example would like this…</p>
<pre class="brush: csharp;">[Binding]
public class ResetPasswordSteps
{
    private ActionResult result;
    private readonly Mock&lt;ISecurityService&gt;;
    private SecurityController controller;
    private User user;

    [BeforeScenario]
    public void BeforeScenario()
    {
       this.securityService = new Mock&lt;ISecurityService&gt;();

       this.controller = new SecurityController(
          this.securityService.Object);

       this.user = new User { Id = 1 };
       this.securityService
          .Setup(s =&gt; s.GetSignedInUser())
          .Returns(this.user);
    }

    [Given(@&quot;I have entered a password with only 5 characters&quot;)]
    public void GivenIHaveEnteredAPasswordWithOnly5Characters()
    {
        ScenarioContext.Current[&quot;Password&quot;] = &quot;Foot&quot;;
    }

    [When(@&quot;I hit save&quot;)]
    public void WhenIHitSave()
    {
        // todo: simulate the form value being posted
        this.result = this.controller.ResetPassword();
    }

    [Then(@&quot;I see a validation error against password&quot;)]
    public void ThenISeeAValidationErrorAgainstPassword()
    {
        var view = this.result as ViewResult;
        Assert.IsNotNull(view);
        Assert.AreEqual(&quot;ResetPassword&quot;, view.ViewName);
        Assert.IsFalse(view.ViewData.ModelState.IsValidField(&quot;Password&quot;);
    }
}</pre>
<p>First we set define a method decorated with the BeforeScenario attribute. This is a Specflow attribute that indicates the method should get executed before each scenario. This can be used to carry out any initialisation required. In this case, I’m mocking the SecurityService dependency and setting it up to return a dummy User object when the GetSignedInUser method gets called.</p>
<p>Executing the NUnit test would evaluate each step in the written scenario, discover these methods based on the binding attributes, and execute them in turn. Any state information that needs to be preserved can be either stored in private fields or on the  ScenarioContext object.</p>
<p>The bit missing is how to simulate a form value being posted in the unit test. Remember the binders will use ValueProviders to get information from the request, so this is the piece we need to mock or simulate. To get this working, we would need to add the following code…</p>
<pre class="brush: csharp;">[When(@"I hit save")]
public void WhenIHitSave()
{
   var form = new FormCollection
   {
      { "Password", (string)ScenarionContext["Password"] }
   };

   this.controller.ValueProvider = form.ToValueProvider();
   this.result = this.controller.ResetPasswordSubmit();
}</pre>
<p>Here we are using the FormCollection class to simulate values being posted, and setting the controller to specifically use this value provider for binding scenarios, instead of the default set of providers.</p>
<p>At this point in time, we get a null reference exception running the test, as TryUpdateModel assumes that the ControllerContext property on the controller is available. To get the test working we need to mock this, so back in our initialisation method we add a few lines…</p>
<pre class="brush: csharp; highlight: [5,6,7,8,9,10];">public void BeforeScenario()
{
   this.controller = new SecurityController(this.securityService.Object);

   var httpContextMock = new Mock&lt;HttpContextBase&gt;();

   this.controller.ControllerContext = new ControllerContext(
      httpContextMock.Object,
      new RouteData(),
      this.controller);

   this.user = new User { Id = 1 };
   this.securityService
      .Setup(s =&gt; s.GetSignedInUser())
      .Returns(this.user);
}</pre>
<p>That’s all we need. To recap…</p>
<ul>
<li>We started by writing a plain English scenario in Specflow</li>
<li>We created a class to contain the step definitions and added binding attributes to map the Specflow scenario steps to the right methods</li>
<li>We added a method to run before the scenario to initialize the controller, mock the dependencies and provide a ControllerContext</li>
<li>In the WhenIHitSave method, we initialized a FormCollection with simulated form values and set the ValueProvider on the controller to make use of this</li>
<li>In our ‘Then’ method we assert that the view was returned with a ModelState error against the Password field</li>
<li>The end result is an executable acceptance test (by default this will be NUnit unless Specflow is configured otherwise)</li>
</ul>
<p>All mocking examples have used <a href="http://code.google.com/p/moq/">Moq</a> which is awesome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2010/12/16/using-specflow-to-test-a-form-post-scenario-with-asp-net-mvc-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting work on TeamBrain, an open source knowledge repository for project teams</title>
		<link>http://www.tentonnebaby.com/2010/12/14/starting-work-on-teambrain-an-open-source-knowledge-repository-for-project-teams/</link>
		<comments>http://www.tentonnebaby.com/2010/12/14/starting-work-on-teambrain-an-open-source-knowledge-repository-for-project-teams/#comments</comments>
		<pubDate>Tue, 14 Dec 2010 21:44:22 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[specflow]]></category>
		<category><![CDATA[teambrain]]></category>

		<guid isPermaLink="false">http://www.tentonnebaby.com/2010/12/14/starting-work-on-teambrain-an-open-source-knowledge-repository-for-project-teams/</guid>
		<description><![CDATA[I’m increasingly finding that all of the information and knowledge that I want ready access to won’t quite fit in my head at one time. Most technical projects that I’ve worked on over the years have suffered from the same problem but amplified across a team. Typically the team will rely on the knowledge of [...]]]></description>
			<content:encoded><![CDATA[<p>I’m increasingly finding that all of the information and knowledge that I want ready access to won’t quite fit in my head at one time. Most technical projects that I’ve worked on over the years have suffered from the same problem but amplified across a team. Typically the team will rely on the knowledge of individuals, and create quite a critical dependency on key people. There are good practices which can reduce the risk of this, such as pair programming, and documenting system behaviour with automated tests, but it still helps to have instant access to pretty much anything you need to know about a project. The fundamental problem is that documentation is not much fun, and very quickly gets out of date.</p>
<p>Obviously there are solutions to this problem, the foremost being to set up a project wiki. However I’m quite fussy about design and interaction, and the way that software feels when you use it, and I’ve yet to find a wiki product that makes me feel good. I want something that is addictive and satisfying to use, that engages you and makes you want to work with it. The idea being to build a multi-tenant application that can host project knowledge for teams.</p>
<p>Based on this I’ve started work on <a href="http://teambrain.codeplex.com">TeamBrain</a>, hosted on Codeplex. Feel free to grab a copy of the source and talk to me as it develops. I have some time off work so will be trying to flesh it out over the next few weeks.</p>
<p>I’m also taking the opportunity to try out a few new technologies and things I’ve wanted to take a look at for a while. The one I’m most excited about is BDD (Behaviour Driven Design). I’ll be running the project by defining features and scenarios in <a href="http://specflow.org/">SpecFlow</a>, and having all of the unit tests and behaviour pulling from the defined scenarios. If you want to learn more, I suggest starting with <a href="http://specflow.org/specflow/screencast.aspx">this screencast</a> which gives a really great introduction. I’ll post some examples and hints as I learn along the way.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2010/12/14/starting-work-on-teambrain-an-open-source-knowledge-repository-for-project-teams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recommended reading for December</title>
		<link>http://www.tentonnebaby.com/2010/11/26/recommended-reading-for-december/</link>
		<comments>http://www.tentonnebaby.com/2010/11/26/recommended-reading-for-december/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 18:37:34 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[CSS / HTML]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[reading]]></category>

		<guid isPermaLink="false">http://www.tentonnebaby.com/2010/11/26/recommended-reading-for-december/</guid>
		<description><![CDATA[C# In Depth By Jon Skeet

I’ve been waiting a while to for the updated edition of C# in Depth by Jon Skeet which is now on sale and covers C#4. I’ve read the first couple of chapters and would really recommend it regardless of how much experience you have with C#. It’s already helped me [...]]]></description>
			<content:encoded><![CDATA[<h2><a href="http://www.manning.com/skeet2/">C# In Depth</a> By <a href="http://msmvps.com/blogs/jon_skeet/Default.aspx">Jon Skeet</a></h2>
<h3><img style="display: inline; border-width: 0px;" title="C# In Depth by Jon Skeet" src="http://www.tentonnebaby.com/wp-content/uploads/2010/11/image.png" border="0" alt="C# In Depth by Jon Skeet" width="207" height="258" /></h3>
<p>I’ve been waiting a while to for the updated edition of <a href="http://www.manning.com/skeet2/">C# in Depth by Jon Skeet</a> which is now on sale and covers C#4. I’ve read the first couple of chapters and would really recommend it regardless of how much experience you have with C#. It’s already helped me with some very clear explanations for concepts that can be a little tricky to express even if you understand them already. One particular explanation that I liked was to distinguish between delegate types and instances, and to think about a delegate type as an interface for a method – so a contract that the method has to fulfil. I’m quite sure that there will be a lot that I’ll get out of reading the rest of the book.</p>
<h2><a href="http://books.alistapart.com/products/css3-for-web-designers">CSS3 For Web Designers</a> by <a href="http://simplebits.com/">Dan Cederholm</a></h2>
<p><img style="display: inline; border-width: 0px;" title="CSS3 for Web Designers by Dan Cederholm" src="http://www.tentonnebaby.com/wp-content/uploads/2010/11/image1.png" border="0" alt="CSS3 for Web Designers by Dan Cederholm" width="537" height="248" /></p>
<p>This is the second book to be published by A Book Apart and should definitely be worth checking out. I think it will really help to provide practical advice about where you can safely start applying elements of CSS3 right now. I’ll post more when I’ve got hold of a copy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2010/11/26/recommended-reading-for-december/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enforcing coding standards when working with legacy code</title>
		<link>http://www.tentonnebaby.com/2010/10/17/enforcing-coding-standards-working-with-legacy-code/</link>
		<comments>http://www.tentonnebaby.com/2010/10/17/enforcing-coding-standards-working-with-legacy-code/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 17:15:52 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[codingstandards]]></category>
		<category><![CDATA[resharper]]></category>
		<category><![CDATA[stylecop]]></category>

		<guid isPermaLink="false">http://www.tentonnebaby.com/2010/10/17/enforcing-coding-standard-working-with-legacy-code/</guid>
		<description><![CDATA[I’ve been thinking about the best way to start getting a consistent coding style with our team. Mostly I’ve worked on new projects and this is a relatively trivial thing to do by setting up StyleCop with an agreed set of rules, and hooking it up to analyse all source files as part of the [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been thinking about the best way to start getting a consistent coding style with our team. Mostly I’ve worked on new projects and this is a relatively trivial thing to do by setting up <a href="http://stylecop.codeplex.com/">StyleCop</a> with an agreed set of rules, and hooking it up to analyse all source files as part of the Continuous Build. Along with a culture based on sarcasm and peer abuse, this usually works out fine.</p>
<p>However in this instance I’m working with a large legacy codebase. Even with the best intentions people won’t stick to a convention unless it gets enforced, and based on the amount of code that doesn’t comply it isn’t very simple to start enforcing the standard.</p>
<p>The only solution I could really see was to swallow the pain and go and reformat all of the existing code, then start enforcing analysis on the projects that have been updated.</p>
<p>I don’t think this would have even been possible if it hadn’t been for <a href="http://www.jetbrains.com/resharper/">Resharper 5</a> and the <a href="http://stylecopforresharper.codeplex.com/">StyleCop for Resharper</a> plugin. I’ve become massively more productive since using Resharper a few weeks back and in this instance it’s been simply awesome.</p>
<h3></h3>
<h2>Code Cleanup</h2>
<p>Once Resharper has been configured with the StyleCop plugin so all coding style conventions are set up, you can run the code cleanup feature against a file or folder (Ctrl-Shift-Alt-F) which will reformat all existing code based on the configured rules. In my case this will typically reduce a file from around 300 violations to about 8. You can set up a profile to control exactly what does or does not get changed as part of this process.</p>
<h2>Cycle through code issues (F12)</h2>
<p>The next most useful feature is that StyleCop violations are treated as code issues by ReSharper. Hitting F12 repeatedly within a code file will jump to the next issue making it very simple to navigate around all violations and clean them up.</p>
<h2>Violation highlighting within editor</h2>
<p>The plugin will also highlight any style violation as you code, and provide tooltips and messages in the status bar to indicate what the violation was.</p>
<p><a href="http://www.tentonnebaby.com/wp-content/uploads/2010/10/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.tentonnebaby.com/wp-content/uploads/2010/10/image_thumb.png" width="562" height="257" /></a> </p>
</p>
<p>It will still take a while to finish updating the existing codebase, but I’m not even sure it would be feasible to attempt this without these tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2010/10/17/enforcing-coding-standards-working-with-legacy-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automating acceptance tests with BDD</title>
		<link>http://www.tentonnebaby.com/2010/06/19/automating-acceptance-tests-with-bdd/</link>
		<comments>http://www.tentonnebaby.com/2010/06/19/automating-acceptance-tests-with-bdd/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 11:22:37 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[specflow]]></category>
		<category><![CDATA[tekpub]]></category>

		<guid isPermaLink="false">http://www.tentonnebaby.com/2010/06/19/automating-acceptance-tests-with-bdd/</guid>
		<description><![CDATA[I think we’re now running a pretty well-structured agile process, but one area that I’m interested in tightening up is how to express acceptance tests for user stories, and ideally how to automate the tests. A lot of the agile reading material talks in passing about acceptance tests without any real definition of the vocabulary [...]]]></description>
			<content:encoded><![CDATA[<p>I think we’re now running a pretty well-structured agile process, but one area that I’m interested in tightening up is how to express acceptance tests for user stories, and ideally how to automate the tests. A lot of the agile reading material talks in passing about acceptance tests without any real definition of the vocabulary or approach to use in expressing, organising and executing tests.</p>
<p>This fits in very nicely with some of the ideas in Behaviour Driven Design, which is like the next iteration of thinking around TDD. The idea is to express acceptance tests using a simple DSL vocabulary working in plain English, then hook up each acceptance test scenario to some automated tests which prove the scenario works as expected.</p>
<p>A simple example would be…</p>
<p><strong>+Title: Customer withdraws cash+     <br /></strong><em>As a</em> customer,    <br /><em>I want</em> to withdraw cash from an ATM,    <br /><em>so that</em> I don’t have to wait in line at the bank.</p>
<p>In BDD, an acceptance test would be along the lines of…</p>
<p><strong>+Scenario 1: Account is in credit+     <br /></strong><em>Given</em> the account is in credit    <br /><em>And</em> the card is valid    <br /><em>And</em> the dispenser contains cash    <br /><em>When</em> the customer requests cash    <br /><em>Then</em> ensure the account is debited    <br /><em>And</em> ensure cash is dispensed    <br /><em>And</em> ensure the card is returned</p>
<p>I’ve lifted the example here from the <a href="http://blog.dannorth.net/introducing-bdd/">key article published on BDD from Dan North</a> – if you’re interested so far then you should stop reading now and go and check out the article.</p>
<p>BDD frameworks will then map each step expressed in the text above through to a method in a test class, so you have a great balance between expressing the acceptance tests in simple language, but having the ability to execute the tests.</p>
<p>I found the best way to learn about this so far was a TekPub video – you’ll need a subscription, but the video is under the ‘Concepts’ section &gt; <a href="http://tekpub.com/view/concepts/5">Concepts:5 Behaviour-driven Design with Specflow</a>.</p>
<p>Overall it seems like a more natural way to approach proving the application behaviour, and makes it much easier for testers and analysts to work alongside you in defining acceptance tests. I’m quite keen to roll this into our day-to-day process.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2010/06/19/automating-acceptance-tests-with-bdd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

