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); }
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)); }
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)); }
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); }