【问题标题】:How can I check if a particular property setter is called on a Mock object?如何检查是否在 Mock 对象上调用了特定的属性设置器?
【发布时间】:2011-10-11 12:03:30
【问题描述】:

对于下面给定的模拟对象,我如何检查 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
    }

【问题讨论】:

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


【解决方案1】:

来自Here

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

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

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

【讨论】:

    【解决方案2】:

    在 the_ajp 的链接之后我是如何做到的:

    mockCar.AssertWasCalled(car => { var dummy = car.TiresWashed; }, options 
     => options.SetPropertyWithArgument(Arg<object>.Is.Anything));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2012-04-16
      • 2021-06-03
      • 1970-01-01
      • 2010-09-13
      • 2015-10-27
      相关资源
      最近更新 更多