Exemple #1
0
        public async Task Lazy_connection_is_async_opened_and_closed_when_necessary()
        {
            using (var connection = new FakeConnection(
                       CreateOptions(new FakeOptionsExtension1 {
                ConnectionString = "Database=FrodoLives"
            })))
            {
                Assert.Equal(0, connection.CreateCount);

                var cancellationToken = new CancellationTokenSource().Token;
                await connection.OpenAsync(cancellationToken);

                Assert.Equal(1, connection.CreateCount);

                var connectionMock = Mock.Get(connection.DbConnection);
                connectionMock.Verify(m => m.OpenAsync(cancellationToken), Times.Once);

                await connection.OpenAsync(cancellationToken);

                await connection.OpenAsync(cancellationToken);

                connectionMock.Verify(m => m.OpenAsync(It.IsAny <CancellationToken>()), Times.Once);

                connection.Close();
                connection.Close();

                connectionMock.Verify(m => m.OpenAsync(It.IsAny <CancellationToken>()), Times.Once);
                connectionMock.Verify(m => m.Close(), Times.Never);

                connection.Close();

                connectionMock.Verify(m => m.OpenAsync(It.IsAny <CancellationToken>()), Times.Once);
                connectionMock.Verify(m => m.Close(), Times.Once);

                await connection.OpenAsync(cancellationToken);

                connectionMock.Verify(m => m.OpenAsync(cancellationToken), Times.Exactly(2));

                connection.Close();

                connectionMock.Verify(m => m.OpenAsync(cancellationToken), Times.Exactly(2));
                connectionMock.Verify(m => m.Close(), Times.Exactly(2));
            }
        }
        public async Task Lazy_connection_is_async_opened_and_closed_when_necessary()
        {
            using (var connection = new FakeConnection(
                CreateOptions(new FakeOptionsExtension1 { ConnectionString = "Database=FrodoLives" })))
            {
                Assert.Equal(0, connection.CreateCount);

                var cancellationToken = new CancellationTokenSource().Token;
                await connection.OpenAsync(cancellationToken);

                Assert.Equal(1, connection.CreateCount);

                var connectionMock = Mock.Get(connection.DbConnection);
                connectionMock.Verify(m => m.OpenAsync(cancellationToken), Times.Once);

                await connection.OpenAsync(cancellationToken);
                await connection.OpenAsync(cancellationToken);

                connectionMock.Verify(m => m.OpenAsync(It.IsAny<CancellationToken>()), Times.Once);

                connection.Close();
                connection.Close();

                connectionMock.Verify(m => m.OpenAsync(It.IsAny<CancellationToken>()), Times.Once);
                connectionMock.Verify(m => m.Close(), Times.Never);

                connection.Close();

                connectionMock.Verify(m => m.OpenAsync(It.IsAny<CancellationToken>()), Times.Once);
                connectionMock.Verify(m => m.Close(), Times.Once);

                await connection.OpenAsync(cancellationToken);

                connectionMock.Verify(m => m.OpenAsync(cancellationToken), Times.Exactly(2));

                connection.Close();

                connectionMock.Verify(m => m.OpenAsync(cancellationToken), Times.Exactly(2));
                connectionMock.Verify(m => m.Close(), Times.Exactly(2));
            }
        }