예제 #1
0
        public void TestTransactionDisableTrue()
        {
            var transactionManagerMock = new Mock <ITransactionManager>();
            var aspectContextMock      = new Mock <AspectContext>();
            var mockDelegateMock       = new Mock <AspectDelegate>();
            var dbOptionMock           = new Mock <DbOption>();
            var tt = new TransactionAttribute
            {
                IsDisable = true
            };

            tt.Invoke(aspectContextMock.Object, mockDelegateMock.Object);
            mockDelegateMock.Verify(t => t.Invoke(aspectContextMock.Object));
        }
예제 #2
0
        public void TestTransactionRollback()
        {
            var transactionManagerMock = new Mock <ITransactionManager>();
            var aspectContextMock      = new Mock <AspectContext>();
            var mockDelegateMock       = new Mock <AspectDelegate>();
            var dbProviderMock         = new Mock <IDbProvider>();
            var factoryMock            = new Mock <ITransactionFactory>();
            var dbOptionMock           = new Mock <DbOption>();
            var transactionMock        = new Mock <ITransaction>();
            var dbTransactionMock      = new Mock <IDbTransaction>();
            var manager  = new TransactionManager();
            var services = Mock.Of <IServiceProvider>(sp =>
                                                      sp.GetService(typeof(ITransactionManager)) == manager &&
                                                      sp.GetService(typeof(DbOption)) == dbOptionMock.Object &&
                                                      sp.GetService(typeof(ITransactionFactory)) == factoryMock.Object
                                                      );

            factoryMock.SetupSequence(t => t.GetTransaction())
            .Returns(transactionMock.Object)
            .Returns(transactionMock.Object)
            .Returns(transactionMock.Object);
            aspectContextMock.Setup(t => t.ServiceProvider).Returns(services);
            var tt = new TransactionAttribute
            {
                IsDisable = false
            };

            tt.Invoke(aspectContextMock.Object, mockDelegateMock.Object);
            var tt1 = new TransactionAttribute
            {
                IsDisable = false
            };

            Assert.ThrowsExceptionAsync <Exception>(() =>
            {
                return(tt1.Invoke(aspectContextMock.Object, t => { throw new Exception(); }));
            });
            transactionMock.Verify(t => t.RollbackAsync());
        }
예제 #3
0
        public void TestTransactionCommit()
        {
            var transactionManagerMock = new Mock <ITransactionManager>();
            var aspectContextMock      = new Mock <AspectContext>();
            var mockDelegateMock       = new Mock <AspectDelegate>();
            var dbProviderMock         = new Mock <IDbProvider>();
            var factoryMock            = new Mock <ITransactionFactory>();
            var dbOptionMock           = new Mock <DbOption>();
            var transactionMock        = new Mock <ITransaction>();
            var dbTransactionMock      = new Mock <DbTransaction>();
            var manager  = new TransactionManager();
            var services = Mock.Of <IServiceProvider>(sp =>
                                                      sp.GetService(typeof(ITransactionManager)) == manager &&
                                                      sp.GetService(typeof(DbOption)) == dbOptionMock.Object &&
                                                      sp.GetService(typeof(ITransactionFactory)) == factoryMock.Object
                                                      );

            factoryMock.SetupSequence(t => t.GetTransaction())
            .Returns(transactionMock.Object)
            .Returns(transactionMock.Object)
            .Returns(transactionMock.Object);
            aspectContextMock.Setup(t => t.ServiceProvider).Returns(services);
            var tt = new TransactionAttribute
            {
                IsDisable = false
            };

            tt.Invoke(aspectContextMock.Object, mockDelegateMock.Object);
            transactionMock.Verify(t => t.CommitAsync());
            var tt1 = new TransactionAttribute
            {
                IsDisable = false
            };

            tt1.Invoke(aspectContextMock.Object, mockDelegateMock.Object);
            transactionMock.Verify(t => t.CommitAsync(), Times.AtLeast(2));
        }
예제 #4
0
        public async Task TestTransactionDisableFalse()
        {
            var transactionManagerMock = new Mock <ITransactionManager>();
            var aspectContextMock      = new Mock <AspectContext>();
            var mockDelegateMock       = new Mock <AspectDelegate>();
            var dbProviderMock         = new Mock <IDbProvider>();
            var factoryMock            = new Mock <ITransactionFactory>();
            var dbOptionMock           = new Mock <DbOption>();
            var transactionMock        = new Mock <ITransaction>();
            var connectFactoryMock     = new Mock <IConnectFactory>();
            var IdbConnectMock         = new Mock <DbConnection>();
            var IdbTransactionMock     = new Mock <DbTransaction>();
            var services = Mock.Of <IServiceProvider>(sp =>

                                                      sp.GetService(typeof(ITransactionManager)) == transactionManagerMock.Object &&
                                                      sp.GetService(typeof(DbOption)) == dbOptionMock.Object &&
                                                      sp.GetService(typeof(ITransactionFactory)) == factoryMock.Object
                                                      );

            connectFactoryMock.Setup(t => t.CreateAndOpenConnectionAsync()).ReturnsAsync(IdbConnectMock.Object);

            aspectContextMock.Setup(t => t.ServiceProvider).Returns(services);
            factoryMock.SetupSequence(t => t.GetTransaction())
            .Returns(new ORM.Database.Transaction(connectFactoryMock.Object))
            .Returns(new InnerTransaction())
            .Returns(new InnerTransaction());

            var tt = new TransactionAttribute
            {
                IsDisable = false
            };
            await tt.Invoke(aspectContextMock.Object, mockDelegateMock.Object);

            transactionManagerMock.Verify(t => t.AddTransaction(It.IsAny <ORM.Database.Transaction>()));
            transactionManagerMock.Verify(t => t.RemoveTransaction(It.IsAny <ITransaction>()));
        }