It’s not the official 1.0 release yet, but I have just published the first (and hopefully only) preview version of SpecsFor. You can download it right now from the CodePlex site. This release meets most of the goals I laid out in my last post on SpecsFor, and if no bugs are found, SpecsFor Version 1.0 will follow shortly. Read on to find out more!
Main Features
SpecsFor is a Behavior-Driven Development style framework that puts developer productivity ahead of all other goals. The current release features:
- AutoMocking – Easily configure and verify behavior.
- ReSharper Live Templates – Quickly create specs with only a few keystrokes.
- Clean Separation of Test State – Encapsulate test setup and reuse it across as many specs as you like.
- Run The Same Specs Multiple Times With Different Contexts – SpecsFor allows you to assert the same things are true given any number of contexts.
- Mix And Match Contexts – Context can be combined and extended to support complex test setup without code duplication or excess noise in your specs.
- Declarative Context – Context can be established in many ways, including by simply marking your spec class with a special attribute.
- Works With Any NUnit Test Runner – No add-ins are needed, SpecsFor is fully compatible with all popular test runners including TestDriven.NET, Resharper, and TeamCity.
Show Me The Code!
As cool as the feature list is, the code is even cooler. SpecsFor provides multiple ways for you to build out your specifications. The primary and most flexible way is through attaching context to classes via attributes, like so:
[Given(typeof(TheCarIsNotRunning), typeof(TheCarIsParked))] [Given(typeof(TheCarIsNotRunning))] public class when_the_key_is_turned : SpecsFor<Car> { public when_the_key_is_turned(Type[] context) : base(context){} protected override void When() { SUT.TurnKey(); } [Test] public void then_it_starts_the_engine() { GetMockFor<IEngine>() .Verify(e => e.Start()); } }
In this example, the specs will be executed twice, once for the combined context of “TheCarIsNotRunning” and “TheCarIsParked”, then again with just “TheCarIsNotRunning” applied.
If you don’t like attributes, you can opt for a more traditional means of establishing context.
public class when_the_key_is_turned_alternate_style : SpecsFor<Car> { protected override void Given() { Given<TheCarIsNotRunning>(); Given<TheCarIsParked>(); base.Given(); } protected override void When() { SUT.TurnKey(); } [Test] public void then_it_starts_the_engine() { GetMockFor<IEngine>() .Verify(e => e.Start()); } }
Unfortunately this method of context specification does not support running the same spec multiple times with different contexts.
Let’s See It In Action!
The feature list is cool, the code is cooler, but seeing it in action is the best way to see what’s going on:
What’s next?
SpecsFor Version 1.0 will be released in the next few days. It will be available as a downloadable binary as well as a NuGet package. In the meantime, I’m still very interested in feedback on the project. You can drop me a line via the comments below, or you can post on the CodePlex site.