How to clear previous expectations on an object?

前端 未结 3 1171
囚心锁ツ
囚心锁ツ 2020-11-29 02:37

I would like to set up a return value

_stubRepository.Stub(Contains(null)).IgnoreArguments().Return(true);

but then in a specific test, ove

3条回答
  •  情深已故
    2020-11-29 03:29

    There are three ways:

    You can reset the expectations by using BackToRecord

    I have to admit that I never really used it because it is awkward.

    // clear expectations, an enum defines which
    _stubRepository.BackToRecord(BackToRecordOptions.All);
    // go to replay again.
    _stubRepository.Replay();
    

    Edit: Now I use it sometimes, it is actually the cleanest way. There should be an extension method (like Stub) which does it - I think it just got forgotten. I would suggest to write your own.

    You can use Repeat.Any()

    It 'breaks' the order of the stubbed definition and "overrides" previous definitions. But it's somehow implicit. I use it sometimes because it is easy to write.

    _stubRepository.Stub(x => x.Contains(null))
      .IgnoreArguments()
      .Return(false)
      .Repeat.Any();
    

    You can create a new mock

    Trivial, but explicit and easy to understand. It is only a problem if you want to keep plenty of definitions and only change one call.

    _stubRepository = MockRepository.GenerateMock();
    _stubRepository.Stub(x => x.Contains(null))
      .IgnoreArguments()
      .Return(false);
    

提交回复
热议问题