Home :: News ::

SpecsFor revision

15th, November 2011
Ok, I want to quickly revise my last post a little about the coding standards I am using for unit testing after further reading into SpecsFor. I watched this video from the author of SpecsFor in which he gave an example of how they originally used SpecsFor; in my head this was much much better then how I am currently doing it. In all honesty no code has really changed just the layout and positioning of code within the parent test class. Here is a revised snippet of my unit tests for PathFinderInfo:
 

public static class given
{
public abstract class a_target_has_been_set : SpecsFor
{
public a_target_has_been_set() { }
public a_target_has_been_set(Type[] context) : base(context) { }

protected override void Given()
{
SUT.SetTarget(new Vector2(100, 100));

base.Given();
}
}
}

public class when_no_step_has_been_set : given.a_target_has_been_set
{
protected override void When()
{
// TODO: perform logic using the system under test (SUT) property
}

[Test]
public void then_should_be_ready_for_a_step()
{
Assert.IsTrue(SUT.IsReadyForNextStep(new Vector2(0f, 0f)));
}

[Test]
public void then_should_have_a_target()
{
Assert.IsTrue(SUT.TargetPosition.HasValue);
}

[Test]
public void then_should_not_have_a_next_step()
{
Assert.IsFalse(SUT.NextStep.HasValue);
}
}

If you look carefully the revision includes mainly naming changes, let’s have a look at the test class name “public class when_no_step_has_been_set : given.a_target_has_been_set”. The class naming convention is saying “GIVEN: a target is set WHEN no step is set” – the benefit of this is that just looking at the declaration of the class we can see what environment we expect the class to be tested under. Furthermore, moving common code into the given.a_target_has_been_set sub-class means we can reduce our test cases even further by not including constructors and other non-needed code.

0 Comments Posted by Simon Colmer

Comments:


No comments found
Add a comment
Name:
Email:
Comment:
Security:
X-Volt Studios, Copyright 2009. ©