如何检查Mock对象上是否调用了特定的属性setter?

时间:2011-10-11 12:03:30

标签: c# .net unit-testing rhino-mocks

对于下面给出的模拟对象,如何检查WashCar(ICar car)方法是否设置了TiresWashed属性?

public interface ICar 
{
    string Model {get;set;}
    bool TiresWashed {get; set;}
    bool WindowsWashed {get; set; }
}

    [TestMethod]
    public vouid MyUnitTest()
    {
    ICar mockCar = MockRepository.GenerateMock<ICar>();
    CarServiceUtility.WashCar(mockCar);

    //Assert if PrepareCar method is called: (this is why I had mock)
    mockCar.AssertWasCalled(c=>c.PrepareCar());

    //TODO 
    // Assert if mockCar.TiresWashed is set with any value
    }

2 个答案:

答案 0 :(得分:8)

来自Here

mock.AssertWasCalled(x => x.Name ="Bob");

mock.AssertWasCalled(x => x.Name =Arg.Is("Bob"));

mock.AssertWasCalled(x => x.Name =Arg<string>.Is.NotNull);

答案 1 :(得分:0)

在the_ajp的链接为:

后,我是如何设法完成的
mockCar.AssertWasCalled(car => { var dummy = car.TiresWashed; }, options 
 => options.SetPropertyWithArgument(Arg<object>.Is.Anything));