Geeks With Blogs
Lee Brandt's Blog You're only as smart as your last line of code

To ease the transition from TDD to BDD, I have built my own little base class and stolen Scott Bellware’s extensions from SpecUnit.net to make my own BDD style specification framework. It keeps things simple and means very little transition pain from the NUnit testing we were doing.

The base class is called SpecificationBase. It simply ties into the NUnit test methods.

 

   1:  using NUnit.Framework;
   2:   
   3:  namespace Application.Specifications.Utilities
   4:  {
   5:      [TestFixture]
   6:      public abstract class SpecificationBase
   7:      {
   8:          #region Setup for translation to BDD terms
   9:          [TestFixtureSetUp]
  10:          public virtual void FixtureSetup()
  11:          {
  12:              SharedContext();
  13:          }
  14:   
  15:          [SetUp]
  16:          public virtual void Setup()
  17:          {
  18:              Context();
  19:              Action();
  20:          }
  21:   
  22:          [TearDown]
  23:          public virtual void TearDown()
  24:          {
  25:              ContextCleanUp();
  26:          }
  27:   
  28:          [TestFixtureTearDown]
  29:          public virtual void FixtureCleanUp()
  30:          {
  31:              SharedContextCleanUp();
  32:          }
  33:          #endregion
  34:   
  35:          #region BDD virtual methods
  36:   
  37:          protected virtual void SharedContext(){}
  38:   
  39:          protected virtual void Context(){}
  40:   
  41:          protected virtual void Action(){}
  42:   
  43:          protected virtual void ContextCleanUp(){}
  44:   
  45:          protected virtual void SharedContextCleanUp(){}
  46:          #endregion
  47:      }
  48:  }
 

Using this base class, it makes it easy to write Specs that are very readable. Below are a bunch of specs using the SpecificationBase class for a page that displays the valid possible commands based on the status of the current batch and the user loading the page.

 
   1:  [Description("Batch Workflow")]
   2:  public class When_managing_a_batch : SpecificationBase
   3:  {
   4:    protected override void SharedContext()
   5:    {
   6:    }
   7:    
   8:    protected override void Context()
   9:    {
  10:    } 
  11:   
  12:    protected override void Action()
  13:    {
  14:    }
  15:    
  16:    protected override void ContectCleanUp()
  17:    {
  18:    }
  19:   
  20:    protected override void SharedContextCleanUp()
  21:    {
  22:    }
  23:   
  24:    [Test]
  25:    public void Should_get_batch()
  26:    {
  27:    }
  28:   
  29:    [Test]
  30:    public void Should_get_current_user()
  31:    {
  32:    }
  33:   
  34:    [Test]
  35:    public void Should_get_valid_commands()
  36:    {
  37:    }
  38:   
  39:    [Test]
  40:    public void Should_display_valid_commands()
  41:    {
  42:    }
  43:  }

 

I was not able to get away from the TestAttribute in NUnit. I use R# to run my specs and don’t have TD.Net, so in order for R# to recognize the specs for running, I have to have the Test attribute on them. I could use:

using Specification = NUnit.Framework.TestAttribute;

this would allow my to get past this, but it would have to be in every specification class, it doesn’t work in the base class, so I chose to ignore the fact that it says “Test” and just read it as “Specification”. (It’s a good brain exercise anyway to begin seeing Tests as Specifications.)

Eventually, I’d like to transition to MSpec. It will take me some time to get my head around everything that is going on in MSpec, though (=()=>, static fields, etc)

Go to part 2!

~L

Posted on Friday, January 16, 2009 11:08 PM BDD | Back to top


Comments on this post: Behavior Driven Development Part 1 of n

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Lee Brandt | Powered by: GeeksWithBlogs.net