views:

157

answers:

1

How do I test the scope of a registered type with structureMap?

For instance I have a registry:

 public class DataRegistry : Registry
 {
    public DataRegistry()
    {
        ForRequestedType<ISessionManager>().TheDefaultIsConcreteType<SessionManager>().CacheBy(StructureMap.Attributes.InstanceScope.Singleton);
        ForRequestedType<ISessionRequest>().TheDefaultIsConcreteType<SessionRequest>().CacheBy(StructureMap.Attributes.InstanceScope.HttpContext);
    }
 }

And I want to test that ISessionRequest instance scope is HttpContext. So I have a test:

    [Test]
    public void Container_AlwaysHas_OneSessionRequestPerHTTPContext()
    {
        //Setup

        //Act
        ObjectFactory.Configure(r => r.AddRegistry<DataRegistry>());

        //Test
    }

But I don't know to test the scope of the type once it's registered?

+2  A: 

I believe you wish to write a test proving that the lifecycle of a registered plugin has the desired lifecycle.

You can inspect the model of the container. Here is one of the tests you wish to write.

[TestFixture]
public class container_registration
{
    [Test]
    public void session_request_should_be_scopped_per_httpcontext()
    {
        var container = new Container(new DataRegistry());

        var plugin = container.Model.PluginTypes.First(p => p.PluginType.UnderlyingSystemType == typeof(ISessionRequest));

        plugin.Lifecycle.ShouldBeOfType(typeof(HttpContextLifecycle));
    }
}
KevM
That looks like it. Much obliged
JimmyP
Sorry it seems that LifeCycle is no longer a member of PluginTypeConfiguration... maybe it has been deprecated or renamed, any ideas? Cant seem to view the structuremap api docs either they're all garbled...
JimmyP
Sorry, I forgot to mention that I was using StructureMap's trunk could be this is a new property?
KevM
Ok, I guess so thanks
JimmyP