Esempio n. 1
0
        public void ClonedObjectContext_disposes_of_connections_in_correct_order()
        {
            var myConnectionInterceptor = new ConnectionDisposingInterceptor();

            DbInterception.Add(myConnectionInterceptor);
            try
            {
                var mockClonedContext = new Mock <ObjectContextProxy>();

                var storeConnection      = new SqlConnection();
                var mockEntityConnection = new Mock <EntityConnectionProxy>();
                mockEntityConnection.Setup(m => m.StoreConnection).Returns(storeConnection);

                mockEntityConnection.Setup(m => m.CreateNew(It.IsAny <SqlConnection>())).Returns <SqlConnection>(
                    c =>
                {
                    var mockClonedConnection = new Mock <EntityConnectionProxy>();
                    mockClonedConnection.Setup(cc => cc.StoreConnection).Returns(c);
                    mockClonedConnection.Setup(m => m.Dispose()).Callback(() => myConnectionInterceptor.IsClonedEntityConnectionDisposed = true);

                    return(mockClonedConnection.Object);
                });

                var mockContext   = CreateMockObjectContext(mockEntityConnection, mockClonedContext);
                var clonedContext = new ClonedObjectContext(mockContext.Object, null, "Database=PinkyDinkyDo");

                clonedContext.Dispose();
            }
            finally
            {
                DbInterception.Remove(myConnectionInterceptor);
            }
        }
Esempio n. 2
0
        public void Calling_Dispose_on_an_already_disposed_cloned_ObjectContext_does_nothing()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            var connectionIsDisposed = false;

            clonedContext.Connection.Disposed += (_, __) => connectionIsDisposed = true;

            clonedContext.Dispose();
            connectionIsDisposed = false;

            clonedContext.Dispose();

            mockClonedContext.Verify(m => m.Dispose(), Times.Once());
            Assert.False(connectionIsDisposed);
        }
Esempio n. 3
0
        public void Disposing_a_cloned_ObjectContext_disposes_both_the_context_and_the_connection()
        {
            var mockClonedContext = new Mock <ObjectContextProxy>();
            var mockContext       = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, "Database=PinkyDinkyDo");

            var connectionIsDisposed = false;

            clonedContext.Connection.Disposed += (_, __) => connectionIsDisposed = true;

            clonedContext.Dispose();

            mockClonedContext.Verify(m => m.Dispose());
            Assert.True(connectionIsDisposed);
        }
        public void ClonedObjectContext_disposes_of_connections_in_correct_order()
        {
            var myConnectionInterceptor = new ConnectionDisposingInterceptor();
            DbInterception.Add(myConnectionInterceptor);
            try
            {
                var mockClonedContext = new Mock<ObjectContextProxy>();

                var storeConnection = new SqlConnection();
                var mockEntityConnection = new Mock<EntityConnectionProxy>();
                mockEntityConnection.Setup(m => m.StoreConnection).Returns(storeConnection);
                
                mockEntityConnection.Setup(m => m.CreateNew(It.IsAny<SqlConnection>())).Returns<SqlConnection>(
                    c =>
                    {
                        var mockClonedConnection = new Mock<EntityConnectionProxy>();
                        mockClonedConnection.Setup(cc => cc.StoreConnection).Returns(c);
                        mockClonedConnection.Setup(m => m.Dispose()).Callback(() => myConnectionInterceptor.IsClonedEntityConnectionDisposed = true);

                        return mockClonedConnection.Object;
                    });

                var mockContext = CreateMockObjectContext(mockEntityConnection, mockClonedContext);
                var clonedContext = new ClonedObjectContext(mockContext.Object, null, "Database=PinkyDinkyDo");

                clonedContext.Dispose();
            }
            finally
            {
                DbInterception.Remove(myConnectionInterceptor);
            }
        }
        public void Calling_Dispose_on_an_already_disposed_cloned_ObjectContext_does_nothing()
        {
            var mockClonedContext = new Mock<ObjectContextProxy>();
            var mockContext = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, null, "Database=PinkyDinkyDo");

            var connectionIsDisposed = false;
            clonedContext.Connection.Disposed += (_, __) => connectionIsDisposed = true;

            clonedContext.Dispose();
            connectionIsDisposed = false;

            clonedContext.Dispose();

            mockClonedContext.Verify(m => m.Dispose(), Times.Once());
            Assert.False(connectionIsDisposed);
        }
        public void Disposing_a_cloned_ObjectContext_disposes_both_the_context_and_the_connection()
        {
            var mockClonedContext = new Mock<ObjectContextProxy>();
            var mockContext = CreateMockObjectContext(CreateMockConnection(), mockClonedContext);

            var clonedContext = new ClonedObjectContext(mockContext.Object, null, "Database=PinkyDinkyDo");

            var connectionIsDisposed = false;
            clonedContext.Connection.Disposed += (_, __) => connectionIsDisposed = true;

            clonedContext.Dispose();

            mockClonedContext.Verify(m => m.Dispose());
            Assert.True(connectionIsDisposed);
        }