Ejemplo n.º 1
0
        public void IncorrectSqlText()
        {
            var connectionFactory = new Mock <IDbConnectionFactory>();
            var connection        = new MockDbConnection();
            var repository        = new SampleRepository(connectionFactory.Object);

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

            repository.GetCars();

            try
            {
                connection.Verify(c => c.Query <Car>(@"incorrect sql", It.IsAny <object>(), It.IsAny <IDbTransaction>(), true, 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.Query<Car>(Match.Create<string>(Predicate<string>, () => ""incorrect sql""), It.IsAny<object>(), It.IsAny<IDbTransaction>(), True, null, null)

Performed invocations:

   Mock<MockDatabase:4> (c):

      MockDatabase.Query<Car>(""select * 
from [Cars] 
order by Make, Model"", null, null, True, null, null)").Using(_comparer));
            }
        }
Ejemplo n.º 2
0
        public void ReturnManyComplexObjects()
        {
            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"
            };
            var ford = new Car
            {
                Registration = "DEF456",
                Make         = "Ford",
                Model        = "Mondeo"
            };

            connectionFactory
            .Setup(f => f.OpenConnection())
            .Returns(connection);
            connection.Setup(c => c.Query <Car>(@"select *
from [Cars]
order by Make, Model", It.IsAny <object>(), It.IsAny <IDbTransaction>(), true, null, null))
            .Returns(new[] { vauxhall, ford });

            var result = repository.GetCars();

            Assert.That(result.Select(c => c.Model), Is.EquivalentTo(new[] { "Astra", "Mondeo" }));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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>()));
        }