Ejemplo n.º 1
0
        public async Task IncorrectParameterType()
        {
            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");

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

Performed invocations:

   Mock<MockDatabase:2> (c):

      MockDatabase.QuerySingleAsync<Car>(""select * 
from [Cars] 
where Registration = @registration"", { registration = reg }, null, null, null)").Using(_comparer));
            }
        }
Ejemplo n.º 2
0
        public async Task VerifyTwoDifferentGenericCallsWithSameSqlWhenSettingEnabled()
        {
            var settings = new Settings
            {
                ResetDapperCachePerCommand = true
            };

            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection(settings);
            var repository        = new SampleRepository(connectionFactory.Object);

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

            await repository.GetCarAsync("reg");

            await repository.GetCarCountAsync("reg");

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

            connection.Verify(c => c.QuerySingleAsync <int>(@"select *
from [Cars] 
where Registration = @registration", It.IsAny <object>(), It.IsAny <IDbTransaction>(), null, null));
        }
Ejemplo n.º 3
0
        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));
            }
        }
Ejemplo n.º 4
0
        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));
        }