Can't stub a ReadOnly property twice?

132 views
Skip to first unread message

Jeffrey Bridgman

unread,
Sep 24, 2012, 2:16:25 PM9/24/12
to rhino...@googlegroups.com
The stripped down example is a bit contrived, but is there any reason I can't stub a property twice and the "last one wins"?
Turning IStatus.Connected into a function produces the same result. VB.Net has the same result. Tested using Rhino Mock 3.6 build 21.

[TestFixture()]
public class TestTests
{
  private IStatus _status;

  [SetUp()]
  public void Setup()
  {
    this._status = MockRepository.GenerateStub<IStatus>();
    this._status.Stub(x => x.Connected()).Return(true);
    // This second stub might lie in a subclass if you're reducing setup code duplication using inheritance
    this._status.Stub(x => x.Connected()).Return(false);
  }

  [Test()]
  public void TestTheTestFramework()
  {
    Assert.IsFalse(this._status.Connected()); // Fails ...
  }
}

Patrick Steele

unread,
Sep 24, 2012, 9:47:03 PM9/24/12
to rhino...@googlegroups.com
Rhino.Mocks allows multiple stubs to be set up on the same method (so
you can test what would happen if the first call returns true and the
second call returns false).

If you want to "reset" and clear out all previous stubs, you could:

status.GetMockRepository().BackToRecordAll();
status.GetMockRepository().ReplayAll();

That should reset everything (i.e. whatever stubs you've previously
set up should be cleared).

---
Patrick Steele
http://weblogs.asp.net/psteele
> --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rhinomocks/-/VeSM6LXIf_MJ.
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to
> rhinomocks+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.

Jeffrey Bridgman

unread,
Nov 1, 2012, 12:58:54 PM11/1/12
to rhino...@googlegroups.com
For simplicity I like to keep the AAA syntax side of things... what you mention looks like the Expect/Replay/Record type stuff I'd like to avoid. Also this blog post says that "It’s important to note that if you do not specify the number of times to repeat for the first stub, then the second stub will simply overwrite the first one", which is contrary to my experience so I was wondering if this was *intended* behavior, a bug, or missing functionality.

I found a more AAA way around this: using .Repeat.Any() for a subsequent call will override a previously stubbed return value where no repeat was specified. But you can only use Repeat.Any() once.
Reply all
Reply to author
Forward
0 new messages