.net - 犀牛模拟 : Is there any way to verify a constraint on an object property's property?

标签 .net rhino-mocks

如果我有

class ObjA {
  public ObjB B;
}
class ObjB {
  public bool Val;
}

class ObjectToMock {
  public DoSomething(ObjA obj){...}
}

有没有什么方法可以定义一个期望,不仅 DoSomething 会被调用,而且 obj.B.Val == true?

我试过了

Expect.Call(delegate { 
    mockObj.DoSomething(null);
}).Constraints(new PropertyIs("B.Val", true));

但无论值是多少,它似乎都失败了。

最佳答案

您可以尝试使用 Is.Matching() 并提供谓词约束(为清楚起见移出行外):

    Predicate nestedBValIsTrue = delegate(ObjA a) { return a.B.Val == true;};
    Expect.Call( delegate {mockobj.DoSomething(null);})
           .Constraints( Is.Matching(nestedBValIsTrue));

关于.net - 犀牛模拟 : Is there any way to verify a constraint on an object property's property?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76584/

相关文章:

c# - 如何使用 MSIX 允许可编辑的 .Net 生成的配置文件?

c# - ASP.NET 页面生命周期 : How Page. ProcessRequest 执行事件和内部方法?

c# - 制定通用和特定的 stub 方法?

c# - 使用 Rhino 模拟模拟 ASPNET MVC DependencyResolver

c# - stub 只读属性的行为

c# - Inno Setup - 具有依赖项的外部 .NET DLL

c# - .Net Core 中的 .AspNetCore.Antiforgery.xxxxxxx cookie 是什么?

c# - 如何仅使用 tasks.json 中的一个命令在 VS Code 中构建所有 C# 项目?

c# - 如何断言一个方法只被调用一次?

c# - 我如何模拟 IQueryable<T>