StructureMap: how to replace an object at runtime

I am trying to inject a laughed instance of ISession (NHibernate) into a structural map. Currently, all this is related to the Bootstrap method, but I want to replace the one that is being mocked. I tried EjectAllInstancesOf, but it threw 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: ​​ . StructureMap.Pipeline.HttpContextLifecycle.findHttpDictionary() C:\DEV\ \StructureMap\Source\StructureMap\Pipeline\HttpContextLifecycle.cs: 50 StructureMap.Pipeline.HttpContextLifecycle.FindCache() C:\DEV\ \StructureMap\Source\StructureMap\Pipeline\HttpContextLifecycle.cs: 28 StructureMap.Pipeline.HttpContextLifecycle.EjectAll() C:\DEV\ \StructureMap\Source\StructureMap\Pipeline\HttpContextLifecycle.cs: 23 StructureMap.Pipeline.HttpLifecycleBase`2.EjectAll() C:\DEV\ \StructureMap\Source\StructureMap\Pipeline\HttpLifecycleBase.cs: 18 StructureMap.InstanceFactory.EjectAllInstances() C:\DEV\ \StructureMap\Source\StructureMap\InstanceFactory.cs: 127 StructureMap.PipelineGraph.EjectAllInstancesOf() C:\DEV\ \StructureMap\Source\StructureMap\PipelineGraph.cs: 193 StructureMap.Container.EjectAllInstancesOf() c:\dev\opensource\structuremap\Source\StructureMap\Container.cs: 393 StructureMap.ObjectFactory.EjectAllInstancesOf() C:\DEV\ \StructureMap\Source\StructureMap\ObjectFactory.cs: 277

+3
2

, (ISession) StructureMap HttpContext, HttpContext unit test. , StructureMap, , , , , NullReferenceException.

, unit test (Boostrapper), ISession Hybrid - HttpContext.

+2

EjectAllInstancesOf(). Inject() , .

0

Source: https://habr.com/ru/post/1752310/


All Articles