示例#1
0
        public void AddDependency_ShouldThrowIfTheServiceInstanceIsSet()
        {
            var svc = new ServiceReference(new AbstractServiceEntry(typeof(IDummyService), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), injector: new Mock <IInjector>(MockBehavior.Strict).Object);

            Assert.DoesNotThrow(() => svc.AddDependency(new ServiceReference(new AbstractServiceEntry(typeof(IDisposable), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), value: new Disposable(), false)));
            svc.Value = new Mock <IDummyService>().Object;

            Assert.Throws <InvalidOperationException>(() => svc.AddDependency(new ServiceReference(new AbstractServiceEntry(typeof(IDisposable), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), value: new Disposable(), false)), Resources.FOREIGN_DEPENDENCY);
        }
示例#2
0
        public void DisposeAsync_ShouldDisposeTheValueAsynchronouslyAndDecrementTheRefCountOfTheDependencies()
        {
            var dependency = new ServiceReference(new AbstractServiceEntry(typeof(IDummyService), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), new Mock <IInjector>(MockBehavior.Strict).Object)
            {
                Value = new Mock <IDummyService>().Object
            };

            var target = new Mock <IAsyncDisposable>(MockBehavior.Strict);

            target
            .Setup(d => d.DisposeAsync())
            .Returns(default(ValueTask));

            var svc = new ServiceReference(new AbstractServiceEntry(typeof(IAsyncDisposable), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), new Mock <IInjector>(MockBehavior.Strict).Object);

            svc.AddDependency(dependency);
            svc.Value = target.Object;

            Assert.That(dependency.RefCount, Is.EqualTo(2));

            svc.DisposeAsync().AsTask().Wait();

            target.Verify(d => d.DisposeAsync(), Times.Once);
            Assert.That(dependency.RefCount, Is.EqualTo(1));
        }
示例#3
0
        public void Dispose_ShouldDisposeTheValueAndDecrementTheRefCountOfTheDependencies()
        {
            var dependency = new ServiceReference(new AbstractServiceEntry(typeof(IDummyService), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), new Mock <IInjector>(MockBehavior.Strict).Object)
            {
                Value = new Mock <IDummyService>().Object // refcount == 1
            };

            var target = new Disposable();

            var svc = new ServiceReference(new AbstractServiceEntry(typeof(IDisposable), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), new Mock <IInjector>(MockBehavior.Strict).Object);

            svc.AddDependency(dependency);
            svc.Value = target;


            Assert.That(dependency.RefCount, Is.EqualTo(2));

            svc.Release();

            Assert.That(target.Disposed);
            Assert.That(dependency.RefCount, Is.EqualTo(1));
        }
示例#4
0
        public void AddDependency_ShouldThrowOnExternallyOwnedService()
        {
            var svc = new ServiceReference(new AbstractServiceEntry(typeof(IDummyService), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), value: new Mock <IDummyService>().Object, false);

            Assert.Throws <InvalidOperationException>(() => svc.AddDependency(new ServiceReference(new AbstractServiceEntry(typeof(IDisposable), null, new Mock <IServiceContainer>(MockBehavior.Strict).Object), value: new Disposable(), false)), Resources.FOREIGN_DEPENDENCY);
        }