I just published a new version of SpecsFor to NuGet.  This version includes a couple of minor enhancements: parameterized context classes and a new Should extension method.

[more]

Parameterized Context

SpecsFor has long had the ability to specify context (ie: the Given step of BDD) in a compositional way using separate context classes.  Prior to version 2.4 though, context classes had to contain a parameterless constructor.  In this new release, you can instantiate a context class manually prior to passing it to SpecsFor.  This means you can create context classes that have constructors with parameters or that provide other means of configuring their behavior.  Here’s an example of how I’m using this feature with SpecsFor.Mvc in a mobile application:

public class when_reviewing_an_invoice : SpecsFor<MvcWebApp>
{
    protected override void Given()
    {
        SUT.NavigateTo<NewInvoiceController>(c => c.ChooseCustomer());
        SUT.FindDisplayFor<ChooseCustomerViewModel>()
            .ListViewFor(m => m.Customers)
            .Items.First(li => li.Text == "Wal-Mart").Click();

        Given(new PartHasBeenAdded("Item 1", "10", "20"));
        Given(new PartHasBeenAdded("Item 2", "20", "30"));
        Given(new PartHasBeenAdded("Item 3", "30", "40"));


        Given(new LaborHasBeenAdded("Labor 1", "1", "2"));
        Given(new LaborHasBeenAdded("Labor 2", "2", "3"));
        Given(new LaborHasBeenAdded("Labor 3", "3", "4"));

        Given(new MiscCostHasBeenAdded("Misc 1", "100", "10"));
        Given(new MiscCostHasBeenAdded("Misc 2", "200", "20"));
        Given(new MiscCostHasBeenAdded("Misc 3", "300", "30"));

        base.Given();
    }
    
    ..snip..
}

String.ShouldContainAll

The other new feature in 2.4 is a new extension method, String.ShouldContainAll.  I found myself repeating “this string should contain all of these substrings” more than a couple of times in the past month, so I rolled that logic into an extension method within SpecsFor.  Here are some tests to prove that it works:

public class when_checking_for_multiple_strings : SpecsFor<string>
{
    protected override void InitializeClassUnderTest()
    {
        SUT = "Test1 Test2 Test3";
    }

    [Test]
    public void then_it_passes_when_the_string_contains_all_the_arguments()
    {
        SUT.ShouldContainAll("Test1", "Test2", "Test3");
    }

    [Test]
    public void then_it_throws_if_the_string_is_missing_any_argument()
    {
        Assert.Throws<AssertionException>(() => SUT.ShouldContainAll("Test1", "Test4"));
    }
}

Like I said, this was certainly not a major release, but the parameterized context classes should open up some new ways of composing your test cases.