我想断言一个方法只被调用一次。我使用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(),我想这个测试失败。
下面是我如何验证一个方法被调用一次。
[Test] public void just_once() { // Arrange var a = MockRepository.GenerateMock<ISomeDataSource>(); a.Expect(x => x.GetSomethingThatTakesALotOfResources()).Return(new Something()).Repeat.Once(); a.Stub(x => x.GetSomethingThatTakesALotOfResources()).Throw(new InvalidOperationException("gotcha")); // Act // First invocation should call GetSomethingThatTakesALotOfResources a.GetMeMyThing(); // Second invocation should return cached result a.GetMeMyThing(); // Assert a.VerifyAllExpectations(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。