tags:

views:

146

answers:

3

I have a registry class like this:

public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        For<IDateTimeProvider>().Singleton().Use<DateTimeProviderReturningDateTimeNow>();
    }

I want to test that the configuration is according to my intent, so i start writing a test:

public class WhenConfiguringIOCContainer : Scenario
{
    private TfsTimeMachine.Domain.StructureMapRegistry registry;
    private Container container;

    protected override void Given()
    {
        registry = new TfsTimeMachine.Domain.StructureMapRegistry();
        container = new Container();
    }

    protected override void When()
    {
        container.Configure(i => i.AddRegistry(registry));
    }

    [Then]
    public void DateTimeProviderIsRegisteredAsSingleton()
    {
        // I want to say "verify that the container contains the expected type and that the expected type
        // is registered as a singleton
    }
}

How can verify that the registry is accoring to my expectations? Note: I introduced the container because I didn't see any sort of verification methods available on the Registry class. Idealy, I want to test on the registry class directly.

+1  A: 

Think of a Registry class like a config file - it doesn't really make sense to test it in isolation, but you might want to test how another class responds to it. In this case, you would test how a Container behaves when given a registry, so you were on the right track by introducing the Container to your test.

In your test, you can request an IDateTimeProvider and assert that the concrete type returned is the type you expect. You can also retrieve 2 instances from the container and assert that they are the same instance (ReferenceEquals) to verify the singleton behavior.

Joshua Flanagan
A: 

@Marius, could you please provide a link containing the source code for the code you added to make the Given When Then possible in your example? Thank you :-)

Atlantez
I've added the required code below, I don't have a link.
Marius
A: 

Here is the code you need to create BDD-style tests with nunit.

  1. The "Then" attribute:

    public class ThenAttribute : TestAttribute {

    }

  2. And the "Scenario" class:

    [TestFixture] public abstract class Scenario { protected abstract void Given();

    protected abstract void When();
    
    
    [SetUp]
    private void Setup()
    {
        Given();
        When();
    }
    

    }

Marius