public async Task VerifyTwoDifferentGenericCallsWithSameSql()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.GetCarAsync("reg");

            Assert.That(
                async() => await repository.GetCarCountAsync("reg"),
                Throws.InvalidOperationException.And.Message.EqualTo(@"Unable to detect the required response type for the command, it could be one of 2 possible options.

Command: 'select * 
from [Cars] 
where Registration = @registration'
Parameters: `registration = reg`
CommandType: Text

To be able to Verify the Dapper call accurately the Command and Parameters (and return type) must be unique for every invocation of a Dapper method.

Possible options: `Dapper.MoqTests.Samples.Car`, `System.Int32`

If this issue cannot be resolved, consider setting `Dapper.MoqTests.Settings.ResetDapperCachePerCommand` to `true`, note this is not a thread-safe approach").Using(_comparer));

            connection.Verify(c => c.QuerySingleAsync <Car>(@"select *
from [Cars] 
where Registration = @registration", It.IsAny <object>(), It.IsAny <IDbTransaction>(), null, null));

            try
            {
                connection.Verify(c => c.QuerySingleAsync <int>(@"select *
from [Cars] 
where Registration = @registration", It.IsAny <object>(), It.IsAny <IDbTransaction>(), null, null));
            }
            catch (System.Exception exc)
            {
                Assert.That(exc.Message.Trim(), Is.EqualTo(@"Expected invocation on the mock at least once, but was never performed: c => c.QuerySingleAsync<int>(Match.Create<string>(Predicate<string>, () => ""select *
from [Cars] 
where Registration = @registration""), It.IsAny<object>(), It.IsAny<IDbTransaction>(), null, null)

Performed invocations:

   Mock<MockDatabase:6> (c):

      MockDatabase.QuerySingleAsync<Car>(""select * 
from [Cars] 
where Registration = @registration"", { registration = reg }, null, null, null)").Using(_comparer));
            }
        }
        public void ExecuteScalar()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            repository.GetModelsCount("Vauxhall");

            connection.Verify(c => c.ExecuteScalar(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <IDbTransaction>(), It.IsAny <int?>(), It.IsAny <CommandType?>()));
        }
        public async Task SupportsStoredProcedures()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.GetModelsSinceAsync("Vauxhall", 2018);

            connection.Verify(c => c.QueryAsync <Car>("sp_getModelsSince", new { make = "Vauxhall", sinceYear = 2018 }, It.IsAny <IDbTransaction>(), It.IsAny <int?>(), CommandType.StoredProcedure));
        }
        public async Task SupportsCommandTimeout()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.GetModelsSinceAsync("Vauxhall", 2018);

            connection.Verify(c => c.QueryAsync <Car>(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <IDbTransaction>(), 100, It.IsAny <CommandType?>()));
        }
        public async Task SupportsTransactionVerificationWithoutSetup()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.DeleteCarAsync("Vauxhall");

            connection.Verify(c => c.ExecuteAsync(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <IDbTransaction>(), null, null));
        }
        public void ExecuteANumberOfTimes()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            repository.DeleteCar("ABC123");

            connection.Verify(c => c.Execute(@"delete from [Cars]
where Registration = @registration", new { registration = "ABC123" }, null, null, null), Times.Once);
        }
        public async Task ExecuteAsync()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.DeleteCarAsync("ABC123");

            connection.Verify(c => c.ExecuteAsync(@"delete from [Cars]
where Registration = @registration", new { registration = "ABC123" }, It.IsAny <IDbTransaction>(), null, null));
        }
        public async Task VerifyQueryAsyncWithParameters()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.GetCarAsync("reg");

            connection.Verify(c => c.QuerySingleAsync <Car>(@"select * 
from [Cars] 
where Registration = @registration", new { registration = "reg" }, It.IsAny <IDbTransaction>(), null, null));
        }
        public async Task VerifyQueryAsync()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.GetCarsAsync();

            connection.Verify(c => c.QueryAsync <Car>(@"select *
from [Cars]
order by Make, Model", It.IsAny <object>(), It.IsAny <IDbTransaction>(), null, null), Times.Once);
        }
Exemple #10
0
        public void VerifyQueryANumberOfTimes()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            repository.GetCars();

            connection.Verify(c => c.Query <Car>(@"select *
from [Cars]
order by Make, Model", It.IsAny <object>(), It.IsAny <IDbTransaction>(), true, null, null), Times.Once);
        }
Exemple #11
0
        public void SupportsMoqArgumentMatches()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);
            connection.Setup(c => c.Query <string>(It.Is <string>(sql => sql.Contains("[Cars]")), It.IsAny <object>(), It.IsAny <IDbTransaction>(), true, null, null))
            .Returns(new[] { "Astra" });

            repository.GetModels("Vauxhall");

            connection.Verify(c => c.Query <string>(It.IsAny <string>(), It.Is <object>(p => p.Prop <string>("make") == "Vauxhall"), It.IsAny <IDbTransaction>(), true, null, null));
        }
Exemple #12
0
        public void VerifyOnly()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            repository.GetCars();

            //NOTE: As there is no setup, you must use <object> in the verify
            connection.Verify(c => c.Query <object>(@"select *
from [Cars]
order by Make, Model", It.IsAny <object>()));
        }
Exemple #13
0
        public async Task ExecuteSingleSerialisedParameter()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);
            var ids = new[] { 1 };

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            await repository.DeleteCarsAsync(ids);

            connection.Verify(c => c.ExecuteAsync(
                                  "delete from [Cars] where Id = @ids",
                                  new { ids },
                                  null,
                                  null,
                                  null));
        }
Exemple #14
0
        public void SupportsMoqArgumentMatches()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);
            var vauxhall          = new Car
            {
                Registration = "ABC123",
                Make         = "Vauxhall",
                Model        = "Astra"
            };

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);
            connection.Setup(c => c.Query <Car>(It.Is <string>(sql => sql.Contains("[Cars]")), It.IsAny <object>()))
            .Returns(new[] { vauxhall });

            repository.GetModels("Vauxhall");

            connection.Verify(c => c.Query <Car>(It.IsAny <string>(), It.Is <object>(p => p.Prop <string>("make") == "Vauxhall")));
        }
Exemple #15
0
        public void QueryMultiple()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);

            repository.GetCarsAndMakes();

            connection.Verify(c => c.Query(
                                  @"select count(*) from [Cars]
                  select count(distinct Make) from [Cars]",
                                  It.IsAny <Func <int, int, object> >(),
                                  It.IsAny <object>(),
                                  It.IsAny <IDbTransaction>(),
                                  It.IsAny <bool>(),
                                  It.IsAny <string>(),
                                  It.IsAny <int?>(),
                                  It.IsAny <CommandType>()));
        }
Exemple #16
0
        public void SetupAndVerify()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);
            var car = new Car
            {
                Registration = "ABC123",
                Make         = "Vauxhall",
                Model        = "Astra"
            };

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);
            connection.Setup(c => c.QuerySingle <Car>(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <IDbTransaction>(), null, null))
            .Returns(car);

            repository.GetCar("ABC123");

            connection.Verify(c => c.QuerySingle <Car>(@"select *
from [Cars]
where Registration = @registration", new { registration = "ABC123" }, It.IsAny <IDbTransaction>(), null, null));
        }