views:

709

answers:

1

I have been using (and liking) the new Rhino Mocks AAA syntax. However, one thing that puzzles me is that I have to create my stubs and mocks like this:

var v1 = MockRepository.GenerateStub<MyClass>();

instead of with an instantiated MockRepository:

var mr = new MockRepository();
var v1 = mr.GenerateStub<MyClass>();

This syntax would make transitioning my unit tests easier.

From reading Ayende's wiki it seems like the second syntax should work, but I just can't get it to function correctly. If I do it that way then I have to use Record/Playback blocks. I also can't find any examples online of anyone using the new syntax without the static methods.

So my question is, has anyone else managed to get the AAA syntax working without using the static methods and without having to resort to Record/Playback? If so, what am I missing?

+2  A: 

I checked rhino mocks with reflector.

MockRepository.GenerateStub actually creates a repository and calls the non static stub. Here is what the static methods actually do (copied from reflector)

public static object GenerateStub(Type type, params object[] argumentsForConstructor)
{
    MockRepository repository = new MockRepository();
    object obj2 = repository.Stub(type, argumentsForConstructor);
    repository.Replay(obj2);
    return obj2;
}

It seems you're missing a call to Repository.Replay in your code.

Mendelt
How come I don't have to call Replay when using the static methods? I don't really how it would be necessary for one and not the other
George Mauer
It's necessary for both. But the static methods call Replay for you.
Mendelt