public async Task Commit_ShouldCommitAndDisposeAllPendingTransactions()
        {
            var wrappedTransactionMocks = new List <Mock <IWrappedTransaction> >();

            var createTransactionMock = new Mock <Func <IEventStoreConnection, string, long, Task <IWrappedTransaction> > >();

            createTransactionMock
            .Setup(x => x(_eventStoreConnectionMock.Object, "some_id", 0))
            .ReturnsAsync(() =>
            {
                var wrappedTransactionMock = new Mock <IWrappedTransaction>();
                wrappedTransactionMocks.Add(wrappedTransactionMock);
                return(wrappedTransactionMock.Object);
            });

            var transaction = new EventStoreTransaction <string, object>(
                connection: _eventStoreConnectionMock.Object,
                jsonSerializerSettings: _jsonSerializerSettings,
                createTransaction: createTransactionMock.Object);

            await transaction.StoreEvents("some_id", 1, new object[] { new EventA(), new EventB() });

            await transaction.StoreEvents("some_id", 1, new object[] { new EventA(), new EventB() });

            await transaction.StoreEvents("some_id", 1, new object[] { new EventA(), new EventB() });

            wrappedTransactionMocks.Should().HaveCount(3);
            wrappedTransactionMocks[0].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[1].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[2].Verify(x => x.Rollback(), Times.Never);

            await transaction.Commit();

            wrappedTransactionMocks[0].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.CommitAsync(), Times.Once);

            wrappedTransactionMocks[0].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[1].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[2].Verify(x => x.Rollback(), Times.Never);

            wrappedTransactionMocks[0].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.Dispose(), Times.Once);

            // Dispose shouldn't have any side effect
            transaction.Dispose();

            wrappedTransactionMocks[0].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.CommitAsync(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.CommitAsync(), Times.Once);

            wrappedTransactionMocks[0].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[1].Verify(x => x.Rollback(), Times.Never);
            wrappedTransactionMocks[2].Verify(x => x.Rollback(), Times.Never);

            wrappedTransactionMocks[0].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[1].Verify(x => x.Dispose(), Times.Once);
            wrappedTransactionMocks[2].Verify(x => x.Dispose(), Times.Once);
        }
Example #2
0
        public void Dispose_Transaction_Should_Dispose_UnitOfWork()
        {
            // Arrange
            Action commit = (() => _uow.Commit());
            var    check1 = false;
            var    check2 = 0;

            _uow.OnCommit += () => check1 = true;
            _uow.OnCommit += () => check2 = 1;
            // Act
            _tran.Dispose();
            _uow.Commit();
            // Assert
            check1.Should().BeFalse();
            check2.Should().Be(0);
        }