加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

tdd – 声明一个方法被调用一次

发布时间:2020-12-14 01:05:02 所属栏目:百科 来源:网络整理
导读:我想断言一个方法只被调用一次。我使用RhinoMocks 3.5。 这里是我认为会工作: [Test]public void just_once(){ var key = "id_of_something"; var source = MockRepository.GenerateStubISomeDataSource(); source.Expect(x = x.GetSomethingThatTakesALotO
我想断言一个方法只被调用一次。我使用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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读