joe.mcbride

Adventures in Silverlight, Windows Phone 7, WPF, and ASP.NET MVC

BDD using SubSpec with xUnit for Silverlight

Download the sample project and source for this post: xUnit-Light with SubSpec

One of the sessions I attended at the Utah Code Camp was “Behavior Driven Development and Executable Specifications in .NET” by David Starr.  This pumped me up to look at BDD again.  I recalled seeing an example of SubSpec for xUnit, though currently the full version of xUnit is desktop only.  So I set out to make the prototype of SubSpec run in Silverlight!  I started from the xUnit-Light built by Jason Jarrett.  I added two classes, ActionTestMethod and ExceptionTestMethod, then altered the TestClass, which was already looking for Fact attributes, to look for Specification attributes as well.

TestSample - Windows Internet Explorer

Here’s an excerpt from the code in TestClass.cs that generates the available test methods:

ICollection specMethods =
	GetMethodsWithAttribute(_type, typeof(SpecificationAttribute));

_tests = new List(specMethods.Count);
foreach (MethodInfo method in specMethods)
{
	try
	{
		object obj = Activator.CreateInstance(method.ReflectedType);
		method.Invoke(obj, null);

		var tests = SpecificationContext.ToTestCommands(method);

		foreach (var test in tests)
		{
			_tests.Add(test);
		}
	}
	catch (Exception ex)
	{
		_tests.Add(new ExceptionTestMethod("GetTestMethods()", ex));
	}
}

And here’s a sample test:

[Specification]
public void PushNullSpecifications()
{
  Stack stack = null;

  "Given a new stack".Context(() => stack = new Stack());

  "with null pushed into it".Do(() => stack.Push(null));

  "the stack is not empty".Assert(() => Assert.False(stack.IsEmpty));
  "the popped value is null".Assert(() => Assert.Null(stack.Pop()));
  "Top returns null".Assert(() => Assert.Null(stack.Top));
}

Which runs the tests:

Given a new stack with null pushed into it, the stack is not empty
Given a new stack with null pushed into it, the popped value is null
Given a new stack with null pushed into it, Top returns null

There’s quite a bit of debate around using this type of syntax on Phil’s post introducing SubSpec, and as I start to create more BDD style tests I may end up agreeing with some of the comments.  Though right now this works great for me.  Johannes Rudolph has also started a new project based on the SubSpec sample which has a few more features.  You can grab the sample project and source code from this post here.

3 Responses to “BDD using SubSpec with xUnit for Silverlight”

  1. [...] This post was mentioned on Twitter by Todd Knudsen, Walter Poch, Larry King, Mariano Ravinale, Joe McBride and others. Joe McBride said: #BDD using #SubSpec with #xunit for #Silverlight http://bit.ly/cqAIli [...]

    Pingback by Tweets that mention BDD using SubSpec with xUnit for Silverlight « joe.mcbride -- Topsy.com — October 3, 2010 @ 7:12 pm

  2. [...] Joe McBride Blog Post on SubSpec / xUnit – http://xamlcoder.com/blog/2010/10/03/bdd-using-subspec-with-xunit-for-silverlight/ [...]

    Pingback by BDD (Behavior Driven Development) with Joe McBride - MindCast Show — October 17, 2010 @ 3:00 pm

  3. [...] Related Posts: BDD Using Subspec with xUnit Lite [...]

    Pingback by MindCast Show–BDD Podcast « joe.mcbride — October 28, 2010 @ 10:07 am

RSS feed for comments on this post. TrackBack URL

Leave a Response