public void SetupOnly() { 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>(@"select * from [Cars] where Registration = @registration", new { registration = "ABC123" }, It.IsAny <IDbTransaction>(), null, null)) .Returns(car); var result = repository.GetCar("ABC123"); Assert.That(result.Make, Is.EqualTo("Vauxhall")); Assert.That(result.Model, Is.EqualTo("Astra")); }
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>())) .Returns(car); repository.GetCar("ABC123"); //NOTE: because the call to QuerySingle() was setup we should use the correct type-argument here connection.Verify(c => c.QuerySingle <Car>(@"select * from [Cars] where Registration = @registration", new { registration = "ABC123" })); }
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)); }