tags:

views:

66

answers:

1

I am trying to inject mocked instance of ISession (NHibernate) to structure map. Currently it all wires it up in a Bootstrap method, but I want to replace the one that is injected with a mocked one. I tried EjectAllInstancesOf but it throw execption.

 [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            Bootstrapper.Bootstrap();
           //TODO: need to remove already wired up types that we are mocking.
            var mockSession = MockRepository.GenerateStub<ISession>();
            var mockLoggerFactory = MockRepository.GenerateStub<ILoggerFactory>();

            ObjectFactory.EjectAllInstancesOf<ISession>();
            ObjectFactory.EjectAllInstancesOf<ILoggerFactory>();

            ObjectFactory.Inject<ISession>(mockSession);
            ObjectFactory.Inject<ILoggerFactory>(mockLoggerFactory);
        }

Error:

System.NullReferenceException: Object reference not set to an instance of an object. at StructureMap.Pipeline.HttpContextLifecycle.findHttpDictionary() in c:\dev\opensource\structuremap\Source\StructureMap\Pipeline\HttpContextLifecycle.cs: line 50 at StructureMap.Pipeline.HttpContextLifecycle.FindCache() in c:\dev\opensource\structuremap\Source\StructureMap\Pipeline\HttpContextLifecycle.cs: line 28 at StructureMap.Pipeline.HttpContextLifecycle.EjectAll() in c:\dev\opensource\structuremap\Source\StructureMap\Pipeline\HttpContextLifecycle.cs: line 23 at StructureMap.Pipeline.HttpLifecycleBase`2.EjectAll() in c:\dev\opensource\structuremap\Source\StructureMap\Pipeline\HttpLifecycleBase.cs: line 18 at StructureMap.InstanceFactory.EjectAllInstances() in c:\dev\opensource\structuremap\Source\StructureMap\InstanceFactory.cs: line 127 at StructureMap.PipelineGraph.EjectAllInstancesOf() in c:\dev\opensource\structuremap\Source\StructureMap\PipelineGraph.cs: line 193 at StructureMap.Container.EjectAllInstancesOf() in c:\dev\opensource\structuremap\Source\StructureMap\Container.cs: line 393 at StructureMap.ObjectFactory.EjectAllInstancesOf() in c:\dev\opensource\structuremap\Source\StructureMap\ObjectFactory.cs: line 277

A: 

Get rid of the calls to EjectAllInstancesOf(). Calling Inject() should do what you want.

Joshua Flanagan
It does not, it actually has two instances, one that was auto-wired and one that is mock. It seems to be the issue only with ISession and the way it is wired up, because I can eject all other types. I ended up ejecting all repositories and replacing them with mocks. See update
epitka