开发者

How can I replace an already declared stub call with a different stub call?

If I have a Rhino Mock object that has already has a stub call declared on it like this:

mockEmploymentService.开发者_C百科Stub(x => x.GetEmployment(999)).Return(employment);

Is there anyway I can remove this call to replace it with something different e.g.:

mockEmploymentService.Stub(x => x.GetEmployment(999)).Return(null);

The reason I ask is that I want to set up some generic mocks to be used in multiple unit tests and then allow each unit test to tailor the calls where necessary.


I use this extension method to clear the behavior of stubs (or the behavior+expectations of mocks):

public static class RhinoExtensions
{
    /// <summary>
    /// Clears the behavior already recorded in a Rhino Mocks stub.
    /// </summary>
    public static void ClearBehavior<T>(this T stub)
    {
        stub.BackToRecord(BackToRecordOptions.All);
        stub.Replay();
    }
}

I picked that up from this other stackoverflow answer, or maybe it was this one.


I use the Repeat.Once() or Repeat.Times(x) methods where it will move on the next stub\expectation when the limit has been reached.


I actually use the stub as a method that receives the expected return and it works.

private void StubDoSomething(bool expected) => Dbs.Stub(x => x.DoSomething(Arg<string>.Is.Anything, Arg<object[]>.Is.Anything)).Return(expected);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜