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