如何检查 Mock 对象是否调用了特定的属性设置器?

发布于 2024-12-09 00:08:50 字数 572 浏览 0 评论 0原文

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

For the given mock object below, how can I check if the WashCar(ICar car) method is setting the TiresWashed property?

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2024-12-16 00:08:50

来自此处

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

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

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

From Here:

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

or

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

or

mock.AssertWasCalled(x => x.Name =Arg<string>.Is.NotNull);
莫多说 2024-12-16 00:08:50

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

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

How I managed to do it after the_ajp's link is:

mockCar.AssertWasCalled(car => { var dummy = car.TiresWashed; }, options 
 => options.SetPropertyWithArgument(Arg<object>.Is.Anything));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文