예제 #1
0
        public async Task TransactionManager_ShouldCallCommitOnSuccessfulAsyncInvocation()
        {
            var mockTransaction = new Mock <IDbTransaction>(MockBehavior.Strict);

            mockTransaction.Setup(tr => tr.Commit());
            mockTransaction.Setup(tr => tr.Dispose());

            var mockDbConnection = new Mock <IDbConnection>(MockBehavior.Strict);

            mockDbConnection
            .Setup(conn => conn.BeginTransaction(IsolationLevel.Unspecified))
            .Returns(mockTransaction.Object);

            var mockModule = new Mock <IModule>(MockBehavior.Strict);

            mockModule
            .Setup(m => m.DoSomethingAsync())
            .Returns(Task.FromResult(1));

            Type proxyType = ProxyGenerator <IModule, TransactionManager <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, new Lazy <IDbConnection>(() => mockDbConnection.Object)) !;

            int result = await module.DoSomethingAsync();

            Assert.That(result, Is.EqualTo(1));

            mockDbConnection.Verify(conn => conn.BeginTransaction(IsolationLevel.Unspecified), Times.Once);
            mockTransaction.Verify(tr => tr.Commit(), Times.Once);
        }
예제 #2
0
        public void TransactionManager_ShouldNotCompleteUntilTheTransactionIsDone()
        {
            bool inTransaction = false;

            var mockTransaction = new Mock <IDbTransaction>(MockBehavior.Strict);

            mockTransaction
            .Setup(tr => tr.Commit())
            .Callback(() => Thread.Sleep(10));
            mockTransaction
            .Setup(tr => tr.Dispose())
            .Callback(() => inTransaction = false);

            var mockDbConnection = new Mock <IDbConnection>(MockBehavior.Strict);

            mockDbConnection
            .Setup(conn => conn.BeginTransaction(IsolationLevel.Unspecified))
            .Returns(() =>
            {
                inTransaction = true;
                return(mockTransaction.Object);
            });

            var mockModule = new Mock <IModule>(MockBehavior.Strict);

            mockModule
            .Setup(m => m.DoSomethingAsync())
            .Returns(async() =>
            {
                await Task.Delay(10);
                return(0);
            });

            Type proxyType = ProxyGenerator <IModule, TransactionManager <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, new Lazy <IDbConnection>(() => mockDbConnection.Object)) !;

            for (int i = 0; i < 100; i++)
            {
                Assert.DoesNotThrowAsync(async() =>
                {
                    await module.DoSomethingAsync();
                    Assert.False(inTransaction);
                });
            }
        }