【问题标题】:How can I check a method is called with a specific parameter on a mock object?如何检查使用模拟对象上的特定参数调用的方法?
【发布时间】:2011-10-10 12:00:24
【问题描述】:

您能否帮助实现我在下面评论块中提到的目标以完成示例单元测试?

想法是如何检查一个模拟对象,如果它的方法之一被调用,一个具有特定属性的类型实例被设置为预期值/

private IMyObject stub = MockRepository.GenerateMock<IMyObject>();
[TestMethod]
public void MakeMyJob_RecievesValidData_CallsRenderWithCorrectParameter()
{
    SomeUtility.MakeMyJob(5,10,stub);

    stub.AsswertWasCalled(s=>s.Render(Arg<IViewModel>.Is. //What next?
                                            // In order to check if Render is called
                                            // with a IViewModel instance 
                                            // whoose Person.Name property is "Peter"

}

【问题讨论】:

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


    【解决方案1】:

    我认为您正在寻找 Rhino Mocks 中的参数约束。前段时间我尝试了一些框架 - link。我想你正在寻找

    Arg&lt;IViewModel&gt;.Matches (vm =&gt; vm.Person.Name == "Peter" )

    【讨论】:

    【解决方案2】:

    reference

     stub.AsswertWasCalled(s=>s.Render(Arg<IViewModel>.Property("Person", "John")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 2016-06-08
      • 2014-09-26
      • 1970-01-01
      • 2020-11-10
      相关资源
      最近更新 更多