<?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; bdd</title>
	<atom:link href="http://www.tentonnebaby.com/tag/bdd/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>Principles of BDD, and the impact on unit testing</title>
		<link>http://www.tentonnebaby.com/2010/12/19/principles-of-bdd-and-the-impact-on-unit-testing/</link>
		<comments>http://www.tentonnebaby.com/2010/12/19/principles-of-bdd-and-the-impact-on-unit-testing/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 21:13:27 +0000</pubDate>
		<dc:creator>Oli</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[requirements]]></category>

		<guid isPermaLink="false">http://www.tentonnebaby.com/2010/12/19/principles-of-bdd-and-the-impact-on-unit-testing/</guid>
		<description><![CDATA[I’ve been working with BDD for a little while now in delivering a practical project, and have started to get a good sense of what is important. Initially I found that a lot of the articles and reading material made sense but were quite abstract. I agreed with the points made in a general sense, [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been working with <abbr title="Behaviour Driven Development">BDD</abbr> for a little while now in delivering a practical project, and have started to get a good sense of what is important. Initially I found that a lot of the articles and reading material made sense but were quite abstract. I agreed with the points made in a general sense, but had some questions around the actual practical application that weren’t necessarily answered. </p>
<h2></h2>
<h2>Everything pulls from the defined scenarios</h2>
<p>The main thing I like about the BDD workflow is that it gives you great structure around how to approach a project. Often when you first start something new it can be a little daunting to know where to begin – whether you start designing a schema, writing some unit tests (which tests, where do you start…), if you begin by writing up some user stories and apply an agile structure around it etc.</p>
<p>The great thing with BDD is that you are either writing some new features or scenarios using a plain English language, or you are implementing tests to deliver those scenarios. Every piece of code that you write and every test should be directly ‘pulling’ from the defined scenarios. As soon as you have finished implementing tests to prove a given scenario, you move on to the next. If you run out of scenarios, you either write some more scenarios for the current feature, or you begin defining a new feature. At any point in time, you pretty much know exactly what to do next.</p>
<h2>How does BDD affect unit tests?</h2>
<p>A big area of confusion that I initially had was how BDD affects unit testing – whether unit tests are effectively replaced by the natural language scenarios, and if you are testing more granular technical detail if you are meant to try to express this from an end user point of view with the scenarios etc.</p>
<p>A user’s experience with software is entirely based on the interactions they have with the user interface. Therefore it makes sense to define the expected behaviours from a user’s point of view, in terms of what the current situation is (Given), what action they take (When) and what the expected result is (Then). </p>
<p>It works quite well to have executable tests for these scenarios to test the user interface, and mock the dependencies. In my project this means testing the MVC controllers and action methods, but mocking the application services. Using a mocking framework you set up the expected behaviour from the dependencies in the ‘Given’ steps, the ‘When’ steps execute the action method on the controller, and the ‘Then’ steps verify things like the ActionResult and the ViewData.</p>
<p>You absolutely need unit tests to verify the behaviour of the lower level components, but the point is that the test scenarios that you write and the expected behaviours are all ‘pulling’ from the requirements in the defined BDD scenarios. If at any point you’re unsure of whether you have the correct set of test cases, you should refer back to the plain English scenarios, read through the expected behaviours, then ensure that your unit tests are set up to verify the correct thing.</p>
<p>This really means that the BDD automated acceptance tests and unit tests do both sit well together. The difference is that the BDD tests give the overall context that you’re working to, and provide a focal point about what you should be testing and why.</p>
<h2>Language used in unit tests</h2>
<p>BDD talks quite a lot about the importance of language used, and how the word ‘test’ implies checking something after the event. Unit tests are all about defining expected behaviours, so the word ‘should’ is much more appropriate. I have to admit, initially I quite seemed like a pretty unimportant distinction to me, and I didn’t quite get it. However I’m now totally convinced.</p>
<p>Previously I would have pretty much created a test fixture per class, and the unit tests names would have probably ended up being oriented around the technical implementation. For example, testing an application service might result in a test fixture called <strong>SecurityServiceTestFixture.</strong></p>
<p>I’ve taken to creating a test fixture for each action or process, then starting each unit test method with ‘<strong>Should&#8217; </strong>to express a particular facet of the expected behaviour. Using the same example, I might create a fixture called <strong>RegisteringANewUser</strong> and the unit test would be called something like <strong>ShouldSendAnEmailToVerifyTheUserEmailAddress.</strong></p>
<p>Although this is quite a subtle distinction it makes it much easier to work out what test cases there should be, and to read back through and understand exactly which behaviours are being tested. It’s also easier to read the original scenario definition, read through your unit tests and get a sense of whether everything is covered.</p>
<h2>Composition of a unit test</h2>
<p>The actual way that a unit test is put together is pretty much unaffected – you’ll generally go with the same arrange, act assert pattern. The only change that I’ve really had to make is organising the test fixtures, and the language that I use for the test methods.</p>
<h2>BDD Workflow</h2>
<p>Putting all of this together gives you the following kind of workflow…</p>
<ul>
<li>You start with the Gherkin based scenarios defined in plain English </li>
<li>You bind each scenario step to an executable method, and test your user interface </li>
<li>During this process you use interfaces for any dependencies, and mock these to verify the UI behaviour </li>
<li>Once the UI tests are passing, you look at each dependency and write lower level unit tests to cover the expected behaviours </li>
<li>For each dependency, you look at any further dependencies and write unit tests to cover these additional components </li>
<li>Work to get each unit test passing </li>
<li>If at any point you get confused about which unit test cases to write, refer back to the scenario definitions to have a clear set of requirements about the expected behaviours </li>
<li>Either move on to the next scenario, or define some more scenarios </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tentonnebaby.com/2010/12/19/principles-of-bdd-and-the-impact-on-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

