public void BeganTransaction_does_not_fail_if_exception_thrown_such_that_there_is_no_transaction()
            {
                var context = MockHelper.CreateMockObjectContext <object>();
                var handler = new CommitFailureHandler();

                handler.Initialize(context);

                var interceptionContext = new BeginTransactionInterceptionContext().WithObjectContext(context);

                Assert.DoesNotThrow(() => handler.BeganTransaction(new Mock <DbConnection>().Object, interceptionContext));
            }
            public void Delegates_to_protected_method()
            {
                var context = MockHelper.CreateMockObjectContext <int>();
                var commitFailureHandlerMock = CreateCommitFailureHandlerMock();

                commitFailureHandlerMock.Object.Initialize(context);
                using (var handler = commitFailureHandlerMock.Object)
                {
                    handler.ClearTransactionHistory();
                    commitFailureHandlerMock.Protected().Verify("PruneTransactionHistory", Times.Once(), true, true);
                }
            }
            public void Delegates_to_protected_method_with_CancelationToken()
            {
                var context = MockHelper.CreateMockObjectContext <int>();
                var commitFailureHandlerMock = CreateCommitFailureHandlerMock();

                commitFailureHandlerMock.Object.Initialize(context);
                using (var handler = commitFailureHandlerMock.Object)
                {
                    var token = new CancellationToken();
                    handler.ClearTransactionHistoryAsync(token).Wait();
                    commitFailureHandlerMock.Protected().Verify <Task>("PruneTransactionHistoryAsync", Times.Once(), true, true, token);
                }
            }
            public void Returns_false_with_ObjectContext_if_nothing_matches()
            {
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(MockHelper.CreateMockObjectContext <object>());

                    Assert.False(
                        handler.MatchesParentContext(
                            new Mock <DbConnection>().Object,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext <object>())
                            .WithDbContext(new DbContext("c"))));
                }
            }
            public void Returns_true_with_ObjectContext_if_no_context_same_connection()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.True(
                        handler.MatchesParentContext(
                            ((EntityConnection)context.Connection).StoreConnection,
                            new DbInterceptionContext()));
                }
            }
            public void Returns_true_with_ObjectContext_if_same_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.True(
                        handler.MatchesParentContext(
                            new Mock <DbConnection>().Object,
                            new DbInterceptionContext().WithObjectContext(context)
                            .WithDbContext(new DbContext("c"))));
                }
            }
            public void Initializes_with_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.Same(context, handler.ObjectContext);
                    Assert.Same(context.InterceptionContext.DbContexts.FirstOrDefault(), handler.DbContext);
                    Assert.Same(((EntityConnection)context.Connection).StoreConnection, handler.Connection);
                    Assert.Same(((EntityConnection)context.Connection).StoreConnection, handler.Connection);
                    Assert.IsType <TransactionContext>(handler.TransactionContext);
                }
            }
            public void Returns_false_with_DbContext_if_different_context_same_connection()
            {
                var connection = CreateMockConnection();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(new DbContext("c"), connection);

                    Assert.False(
                        handler.MatchesParentContext(
                            connection,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext <object>())
                            .WithDbContext(new DbContext("c"))));
                }
            }
            public void Returns_false_with_ObjectContext_if_different_context_same_connection()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = CreateCommitFailureHandlerMock().Object)
                {
                    handler.Initialize(context);

                    Assert.False(
                        handler.MatchesParentContext(
                            ((EntityConnection)context.Connection).StoreConnection,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext <object>())
                            .WithDbContext(new DbContext("c"))));
                }
            }
Exemplo n.º 10
0
            public void Call_to_detect_changes_only_once()
            {
                var objectContextMock = Mock.Get(MockHelper.CreateMockObjectContext <FakeEntity>());

                objectContextMock.Setup(s => s.DeleteObject(It.IsAny <FakeEntity>()));

                var internalContext = new Mock <InternalContextForMock>();

                internalContext.Setup(c => c.ObjectContext)
                .Returns(objectContextMock.Object);

                var set = new InternalSetForTests(internalContext.Object);

                set.RemoveRange(new[] { new FakeEntity(), new FakeEntity() });

                internalContext.Verify(c => c.DetectChanges(It.Is <bool>(b => b == false)), Times.Once());
            }
            public void Throws_if_already_initialized_with_DbContext()
            {
                var dbContext = new DbContext("c");

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(dbContext, dbContext.Database.Connection);

                    var context = MockHelper.CreateMockObjectContext <object>();
                    Assert.Equal(
                        Strings.TransactionHandler_AlreadyInitialized,
                        Assert.Throws <InvalidOperationException>(() => handler.Initialize(context)).Message);

                    Assert.Equal(
                        Strings.TransactionHandler_AlreadyInitialized,
                        Assert.Throws <InvalidOperationException>(() => handler.Initialize(dbContext, dbContext.Database.Connection)).Message);
                }
            }
Exemplo n.º 12
0
        private void VerifyGetter <TProperty>(
            Func <DbQuery <string>, TProperty> getterFunc,
            Expression <Func <IInternalQuery <string>, TProperty> > mockGetterFunc)
        {
            Assert.NotNull(getterFunc);
            Assert.NotNull(mockGetterFunc);

            var internalQueryMock = new Mock <IInternalQuery <string> >();

            internalQueryMock.Setup(m => m.ElementType).Returns(typeof(string));
            internalQueryMock.Setup(m => m.Expression).Returns(Expression.Constant(new object()));
            internalQueryMock.Setup(m => m.InternalContext).Returns(new Mock <InternalContextForMock <DbContext> >().Object);
            internalQueryMock.Setup(m => m.ObjectQueryProvider).Returns(
                new ObjectQueryProvider(MockHelper.CreateMockObjectContext <string>()));
            var dbQuery = new DbQuery <string>(internalQueryMock.Object);

            getterFunc(dbQuery);
            internalQueryMock.VerifyGet(mockGetterFunc, Times.Once());
        }