views:

643

answers:

2

I'm new to mocks and am deciding on a mock framework. The Moq home quotes

Currently, it's the only mocking library that goes against the generalized and somewhat unintuitive (especially for novices) Record/Reply approach from all other frameworks.

Can anyone explain simply what the Record/Replay approach is and how Moq differs? What are the pros and cons of each especially from the point of deciding a framework?

Thanks.

+4  A: 

The Record/Replay approach is used by RhinoMocks. The basic idea is that your test execution is divided into two phases, the record and the replay fase. To be a little bit more concrete

var repo = new MockRepository();
var dependency = repo.DynamicMock<IDependency>();
With.Mocks(repo).Expecting(delegate
             {
              Expect.Call(dependency.AMethod(1)).Return(result);        
             }).Verify(delegate
               {
                var sut = new Sut(wrappee);
                sut.DoStuffThatCallsAMethod();
               Assert.IsTrue(sut.ResultState);
           });

So the Expecting block is the Record phase and the Verify block is the Replay phase.

The Moq variant of this code would be

var dependency = new Mock<IDependency>();
dependency.Expect(dep => dep.AMethod(1)).Returns(result);       
var sut = new Sut(wrappee.Object);
sut.DoStuffThatCallsAMethod();
Assert.IsTrue(sut.ResultState);

Which as you can see is much nicer to read. I used to use RhinoMocks but since I discovered Moq I only use Moq. I find it to be produce much more readable code. So my advice would be to go for Moq.

Bas Bossink
This answer doesn't make much sense to me. Sure, the Moq variant is simpler, but you still have those two phases in the test: line 2 (with the call to "Expect") belongs to the "record" phase, while lines 3 and 4 belong to the "replay" phase. I don't think that mere differences in syntax are essential to what the "record/replay" model is.
Rogerio
A: 

I've gone right off Record/Replay because it makes it hard to see in the setup what's stubbed/mocked or just running code. I believe that Rhino has multiple ways of working. In particular, you can use a using() block to isolate setup from other calls.

Steve Freeman