Example #1
0
        public void Should_throw_exception_when_disposed()
        {
            var sut = new MyDisposableObject();

            sut.Dispose();

            Assert.Throws <ObjectDisposedException>(() => sut.Ensure());
        }
Example #2
0
            public void GivenExistingKey_ThrowsArgumentException()
            {
                var    existingKey = _sut.Keys.ElementAt(0);
                var    newValue    = new MyDisposableObject();
                Action action      = () => _sut.Add(existingKey, newValue, true);

                action.Should().Throw <ArgumentException>();
            }
Example #3
0
        public void Should_dispose_once()
        {
            var sut = new MyDisposableObject();

            sut.Dispose();
            sut.Dispose();

            Assert.True(sut.IsDisposed);

            Assert.Equal(1, sut.DisposeCallCount);
        }
Example #4
0
        public void DisposingParentDisposesChild()
        {
            UnityContainer parent = new UnityContainer();
            IUnityContainer child = parent.CreateChildContainer();

            MyDisposableObject spy = new MyDisposableObject();
            child.RegisterInstance(spy);
            parent.Dispose();

            Assert.IsTrue(spy.WasDisposed);
        }
Example #5
0
        public void DisposingParentDisposesChild()
        {
            UnityContainer  parent = new UnityContainer();
            IUnityContainer child  = parent.CreateChildContainer();

            MyDisposableObject spy = new MyDisposableObject();

            child.RegisterInstance(spy);
            parent.Dispose();

            Assert.IsTrue(spy.WasDisposed);
        }
Example #6
0
            public void WhenItemIsRemovedThatIsMarkedForDisposal_AlsoRemovesThatMark()
            {
                var disposableValue = new MyDisposableObject();

                _sut.Add("D1", disposableValue, true);
                _sut.Disposables.Should().BeEquivalentTo(expectation: new[] { disposableValue });
                _sut.Remove("D1");
                _sut.Disposables.Should().BeEmpty();

                _sut.Add("D1", disposableValue, false);
                _sut.Disposables.Should().BeEquivalentTo(Enumerable.Empty <IDisposable>());
                _sut.Remove("D1");
                _sut.Disposables.Should().BeEquivalentTo(Enumerable.Empty <IDisposable>());
            }
Example #7
0
            public void GivenNewDisposableItem_AddsItemToDictionary()
            {
                var newKey   = "newKey";
                var newValue = new MyDisposableObject();

                _sut.Add(newKey, newValue, true);
                var expectedItems = new[] {
                    new KeyValuePair <string, object>("A", 1),
                    new KeyValuePair <string, object>("B", 2),
                    new KeyValuePair <string, object>(newKey, newValue)
                };
                var actualItems = (ICollection <KeyValuePair <string, object> >)_sut;

                actualItems.Should().BeEquivalentTo(expectedItems);
            }
Example #8
0
        public void CanDisposeChildWithoutDisposingParent()
        {
            MyDisposableObject parentSpy = new MyDisposableObject();
            MyDisposableObject childSpy  = new MyDisposableObject();
            UnityContainer     parent    = new UnityContainer();

            parent.RegisterInstance(parentSpy);
            IUnityContainer child = parent.CreateChildContainer()
                                    .RegisterInstance(childSpy);

            child.Dispose();

            Assert.IsFalse(parentSpy.WasDisposed);
            Assert.IsTrue(childSpy.WasDisposed);

            childSpy.WasDisposed = false;
            parent.Dispose();

            Assert.IsTrue(parentSpy.WasDisposed);
            Assert.IsFalse(childSpy.WasDisposed);
        }
Example #9
0
        public void Run()
        {
            //var obj = new MyDisposableObject();
            //try
            //{

            //    throw new Exception("exception in using");
            //}
            //finally
            //{
            //    obj.Dispose();
            //}



            using (var obj = new MyDisposableObject())
            {
                throw new Exception("exception in using");
                Console.WriteLine("in using block");
            }
            Console.WriteLine("out using block");
        }
Example #10
0
        public void Should_not_throw_exception_when_not_disposed()
        {
            var sut = new MyDisposableObject();

            sut.Ensure();
        }
Example #11
0
        public void CanDisposeChildWithoutDisposingParent()
        {
            MyDisposableObject parentSpy = new MyDisposableObject();
            MyDisposableObject childSpy = new MyDisposableObject();
            UnityContainer parent = new UnityContainer();

            parent.RegisterInstance(parentSpy);
            IUnityContainer child = parent.CreateChildContainer()
                .RegisterInstance(childSpy);
            child.Dispose();

            Assert.IsFalse(parentSpy.WasDisposed);
            Assert.IsTrue(childSpy.WasDisposed);

            childSpy.WasDisposed = false;
            parent.Dispose();

            Assert.IsTrue(parentSpy.WasDisposed);
            Assert.IsFalse(childSpy.WasDisposed);
        }