我必须验证是否使用一系列参数调用目标方法中的方法,如下所示。例如,如何验证RepairCar()
是先用car
参数调用的,而不是car2
.
[TestClass]
public class TestMethodParam
{
[TestMethod]
public void TestMethod1()
{
Mock<ICarService> carService = new Mock<ICarService>(MockBehavior.Strict);
var car = new Car
{
Name = "1"
};
var car2 = new Car
{
Name = "2"
};
var carManager = new CarManager(carService.Object);
//act
carManager.Serve();
//assert:
carService.Verify(c => c.RepairCar(It.Is<Car>(actualCar => actualCar.Name == car.Name)), Times.Once());
carService.Verify(c => c.RepairCar(It.Is<Car>(actualCar => actualCar.Name == car2.Name)), Times.Once());
}
}
public interface ICarService
{
void RepairCar(Car car);
}
public class Car
{
public string Name { get; set; }
}
public class CarManager
{
private ICarService carService;
public CarManager(ICarService carService)
{
this.carService = carService;
}
public void Serve()
{
var car = new Car
{
Name = "1"
};
carService.RepairCar(car);
var car2 = new Car
{
Name = "2"
};
carService.RepairCar(car2);
}
}