DynamicMock and Repeat

43 views
Skip to first unread message

Rob

unread,
May 16, 2008, 5:55:47 PM5/16/08
to Rhino.Mocks
I found this post which appears to answer my quandary of the day:
http://groups.google.com/group/RhinoMocks/browse_thread/thread/ac63910c35f0edb8/0fa0c7cfd01f9b83?lnk=gst&q=dynamicmock+repeat+once#0fa0c7cfd01f9b83

To pull a quote from the post:
"Dynamic Mock will ignore any unknown calls that your make to it.
Therefor, setting up any Repeat means that you are setting a minimum,
not a hard limit."

What I'm wondering is whether or not that is still true. I understand
the desire to have a DynamicMock and to not want to set up each and
every call on it. It seems though that once an expectation is stated,
the "dynamic" component of that call would be lost.

This code demonstates the issue I'm talking about. I would expect the
test to fail, but it passes. If I change the DynamicMock to
CreateMock, the test fails as one would expect.

public interface IMyTestInterface
{
void MyTestMethod();
}

[TestFixture]
public class RepeatTests
{
[Test]
public void Test()
{
var mockery = new MockRepository();
var myMock = mockery.DynamicMock<IMyTestInterface>();

using (mockery.Record())
{
myMock.MyTestMethod();
LastCall.Repeat.Once();
}
using (mockery.Playback())
{
myMock.MyTestMethod();
myMock.MyTestMethod();
}
}
}

Ayende Rahien

unread,
May 17, 2008, 3:08:14 AM5/17/08
to Rhino...@googlegroups.com
The key difference between dynamic and strict mocks is how they treat unexpected method calls.
In your test, the second method call is not expected, and is ignored.

Rob

unread,
May 18, 2008, 3:51:08 PM5/18/08
to Rhino.Mocks
I wouldn't argue with the default behavior of a dynamic mock at all.
That's why we like it so much. The interesting part to me I guess is
the fact that my stating "LastCall.Repeat.Once()" doesn't undo the
dynamic behavior of that particular method call on the dynamic mock.
If that can't happen, I think I would rather get an exception that
tells me that I can't specify "Once" on a dynamic mock, than just have
the test pass as if everything is working as expected.

Not a huge deal by any means. The production code has been updated and
is working like a champ now.

On May 17, 2:08 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> The key difference between dynamic and strict mocks is how they treat
> unexpected method calls.In your test, the second method call is not
> expected, and is ignored.

Ayende Rahien

unread,
May 21, 2008, 2:49:03 PM5/21/08
to Rhino...@googlegroups.com
The problem here is that we have different models on what is going on.

Repeat.One() is actually the default behavior.

The way it works is:


  [Test]
  public void Test()
  {
     var mockery = new MockRepository();
     var myMock = mockery.DynamicMock<IMyTestInterface>();

     using (mockery.Record())
     {
// create an expectation that this method would be call once
        myMock.MyTestMethod();

// doesn't really do anything
        LastCall.Repeat.Once();
     }
     using (mockery.Playback())
     {
// match to the first expectation and satisfy it.
        myMock.MyTestMethod();
// back to usual behavior
        myMock.MyTestMethod();
     }


What you seem to want is something like this

Ayende Rahien

unread,
May 21, 2008, 2:49:11 PM5/21/08
to Rhino...@googlegroups.com
Like this:

MockRepository mocks = new MockRepository();
IDemo demo = mocks.DynamicMock<IDemo>();
demo.VoidNoArgs();
LastCall.Repeat.Once();// doesn't realy matter
demo.VoidNoArgs();
LastCall.Repeat.Never();

mocks.ReplayAll();

demo.VoidNoArgs();//should work

demo.VoidNoArgs();// will fail

Rob

unread,
May 22, 2008, 2:37:49 PM5/22/08
to Rhino.Mocks
I just wanted to say that I really appreciate the feedback. I know
all of our time is valuable.

I'm just arguing for the sake of argument at this point. I know how to
use your framework as intended now and still think it's completely
excellent to use. So, with that said, one more go at it...

I think I would still assert that the definition of Once is "Once and
only once". This is especially so with the presence of another method
called "AtLeastOnce".

Now, given that assertion, if someone creates a dynamic mock that
ignores unexpected calls, and then turns around and says "Once" on one
of the methods of that mock, we're left with a contradiction I think.
For that one method, did the user mean dynamic or did he/she mean
once? I personally would guess that the user meant "dynamic for
everything on the mock except the one where I was specific."

Have an excellent day and thanks again.
Rob
> > On Sun, May 18, 2008 at 10:51 PM, Rob <rten...@gmail.com> wrote:
>
> >> I wouldn't argue with the default behavior of a dynamic mock at all.
> >> That's why we like it so much. The interesting part to me I guess is
> >> the fact that my stating "LastCall.Repeat.Once()" doesn't undo the
> >> dynamic behavior of that particular method call on the dynamic mock.
> >> If that can't happen, I think I would rather get an exception that
> >> tells me that I can't specify "Once" on a dynamic mock, than just have
> >> the test pass as if everything is working as expected.
>
> >> Not a huge deal by any means. The production code has been updated and
> >> is working like a champ now.
>
> >> On May 17, 2:08 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> >> > The key difference between dynamic and strict mocks is how they treat
> >> > unexpected method calls.In your test, the second method call is not
> >> > expected, and is ignored.- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

unread,
May 22, 2008, 7:09:45 PM5/22/08
to Rhino...@googlegroups.com
The problem is with the interpretation of Once.
Reply all
Reply to author
Forward
0 new messages