25

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

mockEmploymentService.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.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Simon Keep
  • 9,886
  • 9
  • 63
  • 78

3 Answers3

21

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.

Community
  • 1
  • 1
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • 3
    Thanks Wim, the only problem is that it would clear all stub calls so I would have to reset them all rather than just override the one I am interested in. – Simon Keep Mar 18 '10 at 13:46
12

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.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Mark Broadhurst
  • 2,675
  • 23
  • 45
1

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);
luturol
  • 565
  • 8
  • 12