Esempio n. 1
0
        public void Verify_ExecutedWithinActiveScope_CreatesScopedInstancesInItsOwnScope()
        {
            // Arrange
            DisposablePlugin plugin = null;

            var container = new Container();

            container.Options.EnableAutoVerification = false;

            var scopedLifestyle = new FakeScopedLifestyle(new Scope(container));

            container.Register <ServiceDependingOn <IPlugin> >();
            container.Register <IPlugin, DisposablePlugin>(scopedLifestyle);
            container.RegisterInitializer <DisposablePlugin>(p => plugin = p);

            // By resolving IPlugin here, it becomes cached within the created Scope.
            container.GetInstance <IPlugin>();
            plugin = null;

            // Act
            // When calling verify, we expect DisposablePlugin to be created again, because verify gets its own scope
            container.Verify();

            // Assert
            Assert.IsNotNull(plugin);
        }
Esempio n. 2
0
        public void Verify_ExecutedWithinActiveScope_DisposesThatInstanceWhenVerifyFinishes()
        {
            // Arrange
            DisposablePlugin plugin = null;

            var container = new Container();

            var lifestyle = new FakeScopedLifestyle(new Scope());

            container.Register <IPlugin, DisposablePlugin>(lifestyle);
            container.RegisterInitializer <DisposablePlugin>(p => plugin = p);

            // Act
            container.Verify();

            // Assert
            Assert.IsNotNull(plugin);
            Assert.IsTrue(plugin.IsDisposedOnce);
        }
        public void Dispose_RecursiveResolveTriggeredInDispose_ThrowsDescriptiveException()
        {
            // Arrange
            var scope = new Scope();

            var scopedLifestyle = new FakeScopedLifestyle(scope);

            var container = new Container();

            container.Register<IPlugin>(() =>
            {
                var plugin = new DisposablePlugin(disposing: _ =>
                {
                    // Recursive dependency
                    // Although really bad practice, this must not cause an infinit spin or a stackoverflow.
                    container.GetInstance<IPlugin>();
                });

                scope.RegisterForDisposal(plugin);

                return plugin;
            });

            container.GetInstance<IPlugin>();

            // Act
            Action action = () => scope.Dispose();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains<ActivationException>(@"
                The registered delegate for type IPlugin threw an exception. A recursive registration of 
                Action or IDisposable instances was detected during disposal of the scope. 
                This is possibly caused by a component that is directly or indirectly depending on itself"
                .TrimInside(),
                action);
        }