【问题标题】:Counting method calls in Rhino Mocks在 Rhino Mocks 中计算方法调用
【发布时间】:2011-07-27 11:21:22
【问题描述】:

所以:我想用比 Any()、Once() 或 AtLeastOnce() 更具体的方法计算 Rhino Mocks 中的方法调用。有没有这样做的机制?

【问题讨论】:

标签: c# rhino-mocks


【解决方案1】:

诀窍是使用 Repeat.Times(n),其中 n 是次数。

令人惊讶的是,即使该方法的调用频率比预期的要高,以下测试也会通过:

[Test]
public void expect_repeat_n_times_does_not_work_when_actual_greater_than_expected() {
  const Int32 ActualTimesToCall = 6;
  const Int32 ExpectedTimesToCall = 4;

  var mock = MockRepository.GenerateMock<IExample>();
  mock.Expect(example => example.ExampleMethod()).Repeat.Times(ExpectedTimesToCall);

  for (var i = 0; i < ActualTimesToCall; i++) {
      mock.ExampleMethod();
  }

  // [?] This one passes
  mock.VerifyAllExpectations();
}

要解决此问题,请使用以下方法:

[Test]
public void aaa_repeat_n_times_does_work_when_actual_greater_than_expected() {
  const Int32 ActualTimesToCall = 6;
  const Int32 ExpectedTimesToCall = 4;

  var mock = MockRepository.GenerateMock<IExample>();

  for (var i = 0; i < ActualTimesToCall; i++) {
      mock.ExampleMethod();
  }

  // This one fails (as expected)
  mock.AssertWasCalled(
      example => example.ExampleMethod(),
      options => options.Repeat.Times(ExpectedTimesToCall)
  );
}

来源:http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/ (在那里寻找解释)

编辑:仅在开始时进行了总结,感谢您的有用回复。

【讨论】:

    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多