Behaviour Driven Development, or BDD, represents the evolution of current coding
practices with respect to Test Driven Development (TDD). Before we introduce BDD
and NSpec, let's take a look at some of the currently perceived problems with TDD.
So, this BDD thing, what's it all about? Well, it takes what developers and coaches
have seen and experienced whilst practicing their art, and applies that learning
to create something that looks and feels quite different:
Now the less interesting bit: NSpec. This is the software that will assis .Net developers
in using BDD on their projects. Let's jump straight in and look at how to write a
specification in NSpec...
Firstly, add a reference in your project to NSpec.Framework, then...
using System;
using
NSpec.Framework;
namespace Example
{
��� [Context]
��� public class Example
��� {
������� [Specification]
������� public void BooleanSpecificationExample()
������� {
����������� //
...Some set up code...
����������� Specify.That(
someObj.SomeMethod() ).ShouldBeFalse();
����������� // Or...
����������� Specify.That(
someObj.SomeOtherMethod() ).ShouldBeTrue();
������� }
������� [Specification]
������� public void EqualitySpecificationExample()
������� {
����������� // ...With
objects...
����������� Specify.That(
someObj.Foo() ).ShouldEqual( someOtherObj );
����������� // ...Or
a with floating point numbers...
����������� Specify.That(
someObj.GetSomeFloat() ).ShouldEqual( 5.3 ).WithAToleranceOf( 0.1 );
������� }
������� [Specification]
������� public void NullSpecificationExample()
������� {
����������� //
...Specify null...
����������� Specify.That(
someObj.SomeMethod() ).ShouldBeNull();
����������� // ...Or
specify not null...
����������� Specify.That(
someObj.SomeFoo() ).ShouldNotBeNull();
������� }
������� [Specification]
������� public void ReferentialEqualityExample()
������� {
����������� Specify.That(
obj.Foo() ).ShouldBeTheSameAs( otherObj.Foo() );
����������� // ...Or
specify inequality...
�������� ��� Specify.That(
obj.Foo() ).ShouldNotBeTheSameAs( otherObj.Foo() );
������� }
������� [Specification]
������� public void TypeEqualityExample()
������� {
����������� Specify.That(
someObj.Bar() ).ShouldBeOfType( typeof( Foo )
);
������� }
������� [Specification]
������� public void ExceptionSpecificationExample()
������� {
����������� MethodThatThrows
mtt = delegate()
����������� {
��������������� // Do
some set up...
��������������� //
Then call the method that should throw the exception.
����� ���������� someObj.MethodThrows();
����������� };
����������� Specify.ThrownBy(
mtt ).ShouldBeOfType( typeof(SomeException) );
������� }
��� }
}
Using this syntax, we can build up a specification suite to be run by the console
spec runner. To run the specs, use the following syntax...
NSpec.Console.exe /assembly:YourAssembly.dll /output:YourPreferredXmlOutFile.xml
To produce...
