<?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; specflow</title>
	<atom:link href="http://www.tentonnebaby.com/tag/specflow/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>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>

