Click here to Skip to main content
13,000,742 members (67,649 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 14 Feb 2010

User Story Source Code Layout with MSpec

, 14 Feb 2010
Rate this:
Please Sign up or sign in to vote.
An example showing a source code structure that reflects user stories using MSpec.


With this article, I am demonstrating a certain source code structure for behavior-driven development (BDD) compatible with many testing frameworks.

One advantage of this style is how it integrates with source code viewing tools such as Solution Explorer and ReSharper (R#). Another is that the tests are very easy to read and understand, even for less-technical people.

For my example, I will use Machine Specifications (MSpec) because I prefer that framework's syntax.


This article is intended for people with some familiarity with BDD. Wikipedia's article on the topic is here and the BDD wiki sits here. Another link that some readers may find helpful is the one I have included on MSpec. A tutorial for wiring R# and MSpec together can be found here.

Using the Code

The code is divided into two projects: Banking (the main project) and Banking.Behavior (the test project).

What I am showing here is the source that mirrors a user story: As a\[Role]\I Want\[Feature]\so that\[Benefit]:

Banking.Behavior\As an\account holder\I want\to transfer 
   money from my savings account\so that\I can get cash easily from an ATM

Typically, a single user story is comprised of many scenarios. Therefore, the source structure mirrors this fact as well: As a\[Role]\I Want\[Feature]\When\[Scenarios]:

Banking.Behavior\As an\account holder\I want\to transfer money 
  from my savings account\When\I_transfer_an_amount_less_than_my_savings_account_balance.cs

Each scenario has one-to-many assertions, which sit inside the associated scenario file:

using Banking;
using Machine.Specifications;
using NUnit.Framework;

namespace When
    public class I_transfer_an_amount_less_than_my_savings_account_balance
        static Account _savings;
        static Account _checking;

        Because of = () =>
            _savings = new Account(100);
            _checking = new Account(10);
            new Bank().Transfer(_savings, _checking, 20);

        It should_subtract_the_amount_from_my_savings_account_balance = () =>
            Assert.That(_savings.Balance, Is.EqualTo(80));

        It should_add_the_amount_to_my_checking_account_balance = () =>
            Assert.That(_checking.Balance, Is.EqualTo(30));

The scenario here is I_transfer_an_amount_less_than_my_savings_account_balance, and the assertions are should_subtract_the_amount_from_my_savings_account_balance and should_add_the_amount_to_my_checking_account_balance. Notice that I have placed this class inside the When namespace. This provides clarity when viewing the source code from a test runner.

I use ReSharper (R#) for my test runner. Here is the view from R#'s 'Unit Test Explorer' with the 'Projects and Namespaces' option:


Here is the view from R#'s Unit Test Sessions with the 'Projects Structure' option:


Here is the view from Solution Explorer:


Note: to run the example project, you will need to include the Machine.Specifications, Machine.Specifications.NUnit, and the NUnit.Framework DLLs yourself. I did not include these DLLs because of potential licensing conflicts between and those products. The MSpec source can be found here (you have to build it yourself), and the NUnit downloads can be found here.

Points of Interest

This source code structure is portable to many other testing frameworks, such as pure NUnit. In the case of pure NUnit, we would port the It delegates to [Test]-decorated methods, the Because delegates to [SetUp]-decorated methods, and add the [TestFixture] attribute to each test class.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Bardy8523-Mar-11 20:42
memberBardy8523-Mar-11 20:42 
GeneralProject structure, NUnit Pin
AlexanderGross14-Feb-10 13:48
memberAlexanderGross14-Feb-10 13:48 
GeneralRe: Project structure, NUnit [modified] Pin
erik.f15-Feb-10 5:18
membererik.f15-Feb-10 5:18 
QuestionWhat is MSpec and why should I care about it? Pin
John Simmons / outlaw programmer14-Feb-10 11:26
memberJohn Simmons / outlaw programmer14-Feb-10 11:26 
AnswerRe: What is MSpec and why should I care about it? Pin
erik.f14-Feb-10 12:04
membererik.f14-Feb-10 12:04 
GeneralCorrect MSpec link Pin
erik.f14-Feb-10 8:02
membererik.f14-Feb-10 8:02 
GeneralRe: Correct MSpec link Pin
erik.f15-Feb-10 5:22
membererik.f15-Feb-10 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 14 Feb 2010
Article Copyright 2010 by erik.f
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid