Exemplo n.º 1
0
        public async Task ExecuteWithRollbackAndResultShouldCommitOnSuccess()
        {
            var connection  = NewOpenConnection();
            var transaction = NewTransaction();

            connection.Setup(x => x.BeginTransaction(It.IsAny <IsolationLevel>())).Returns(transaction.Object);
            transaction.Setup(x => x.Commit());

            var connectionHelper = new DefaultConnectionHelper(() => connection.Object);
            await connectionHelper.ExecuteWithRollbackOnFailureAsync <int>(dbConnection => Task.FromResult(0));

            transaction.Verify(x => x.Commit(), Times.Once);
            connection.Verify(x => x.Dispose(), Times.Once);
        }
Exemplo n.º 2
0
        public async Task ExecuteWithRollbackAndResultShouldRollbackOnFailure()
        {
            var connection  = NewOpenConnection();
            var transaction = NewTransaction();

            connection.Setup(x => x.BeginTransaction(It.IsAny <IsolationLevel>())).Returns(transaction.Object);
            transaction.Setup(x => x.Rollback());

            var connectionHelper = new DefaultConnectionHelper(() => connection.Object);
            await Assert.ThrowsExceptionAsync <Exception>(
                () => connectionHelper.ExecuteWithRollbackOnFailureAsync <int>(
                    dbConnection => Task.FromException <int>(new Exception())));

            transaction.Verify(x => x.Rollback(), Times.Once);
            connection.Verify(x => x.Dispose(), Times.Once);
        }
Exemplo n.º 3
0
 public async Task ExecuteWithRollbackAndReturnShouldThrowWhenActionIsNull()
 {
     var connectionHelper = new DefaultConnectionHelper(() => null);
     await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => connectionHelper.ExecuteWithRollbackOnFailureAsync <int>(null));
 }