简体   繁体   English

Rhino Mocks:如何在期望中匹配数组参数?

[英]Rhino Mocks : How to match array arguments in an expectation?

Again at the Rhino Mocks Noob Wall 再次在犀牛Mo Noob Wall

mockUI.Expect( x => x.Update( new Frame[] {Frame.MakeIncompleteFrame(1, 5)} ) );

This is the exact argument that I need to match. 这是我需要匹配的确切参数。 Via trace statements, I have verified that is the actual output as well ie the code behaves as intended but the test disagrees. 通过跟踪语句,我已经验证了它也是实际的输出,即代码的行为符合预期,但测试不同意。 RhinoMocks responds with RhinoMocks回应

TestBowlingScorer.TestGamePresenter.TestStart:
Rhino.Mocks.Exceptions.ExpectationViolationException : IScoreObserver.Update([Frame# 1, Score =  0 Rolls [  5,  PENDING,  ]]); Expected #1, Actual #0.

A Frame object contains few properties but doesn't override Equals() yet (overridden ToString() seen above). 一个Frame对象包含很少的属性,但是还没有覆盖Equals()(如上所示被覆盖的ToString())。 Update receives an array of Frames; 更新收到一个框架数组; How do I setup this expectation? 我该如何设定这个期望? I see an Is.Matching constraint.. not sure how to use it or rather am concerned with the verbose nature of it. 我看到一个Is.Matching约束..不确定如何使用它,或者更关心它的冗长性质。

I have a helper NUnit style custom Assert 我有一个帮助程序NUnit样式自定义Assert

public static void AssertFramesAreEqual(Frame[] expectedFrames, Frame[] actualFrames)
{
  // loop over both collections
     // compare attributes
}

@Gishu, Yeah, that's it. @Gishu,是的。 I just also learned about the Arg<> static class which should allow you to do something like this: 我也刚刚学习了Arg <>静态类,该类应该允许您执行以下操作:

mockUI.Expect( x => x.Update(Arg<Frame[]>
           .Matches(fs=>HelperPredicates.CheckFrames ( expected, fs)) ));

There is also the Arg<>.List configuration starting point which I have not explored yet but might be even better for what you want 还有一个Arg <>。List配置起点,我还没有探索过,但是对于您想要的东西可能会更好

Verified works.. don't know if this is THE RhinoMocks way 经过验证的作品..不知道这是否是RhinoMocks的方式

var expectedFrames = new Frame[] { Frame.MakeIncompleteFrame(1, 5) };
mockUI.Expect( x => x.Update(null) )
            .IgnoreArguments()
            .Constraints( Is.Matching<Frame[]>( frames => HelperPredicates.CheckFramesMatch(expectedFrames, frames) ) );

The helper predicate is just a function that returns a boolean value - True on an exact match else false. 辅助谓词只是一个返回布尔值的函数-完全匹配时为True,否则为false。

 public static bool CheckFramesMatch(Frame[] expectedFrames, Frame[] actualFrames)
  {
     // return false if array lengths differ
     // loop over corresponding elements
          // return false if any attribute differs
     // return true
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM