【发布时间】:2010-09-22 02:44:42
【问题描述】:
我想断言一个方法只被调用了一次。我正在使用 RhinoMocks 3.5。
以下是我认为可行的方法:
[Test]
public void just_once()
{
var key = "id_of_something";
var source = MockRepository.GenerateStub<ISomeDataSource>();
source.Expect(x => x.GetSomethingThatTakesALotOfResources(key))
.Return(new Something())
.Repeat.Once();
var client = new Client(soure);
// the first call I expect the client to use the source
client.GetMeMyThing(key);
// the second call the result should be cached
// and source is not used
client.GetMeMyThing(key);
}
如果GetMeMyThing() 的第二次调用调用source.GetSomethingThatTakesALotOfResources(),我希望此测试失败。
【问题讨论】:
-
上面唯一的错误是使用 GenerateStub 而不是 GenerateMock 并且缺少最后一行:client.VerifyAllExpectations();
-
...我已经编辑了接受的答案...
标签: tdd mocking rhino-mocks assertions