繁体   English   中英

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

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

再次在犀牛Mo Noob Wall

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

这是我需要匹配的确切参数。 通过跟踪语句,我已经验证了它也是实际的输出,即代码的行为符合预期,但测试不同意。 RhinoMocks回应

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

一个Frame对象包含很少的属性,但是还没有覆盖Equals()(如上所示被覆盖的ToString())。 更新收到一个框架数组; 我该如何设定这个期望? 我看到一个Is.Matching约束..不确定如何使用它,或者更关心它的冗长性质。

我有一个帮助程序NUnit样式自定义Assert

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

@Gishu,是的。 我也刚刚学习了Arg <>静态类,该类应该允许您执行以下操作:

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

还有一个Arg <>。List配置起点,我还没有探索过,但是对于您想要的东西可能会更好

经过验证的作品..不知道这是否是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) ) );

辅助谓词只是一个返回布尔值的函数-完全匹配时为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