views:

25

answers:

2

Hi I have a mock as below:

        MockRepository mocks = new MockRepository();
        ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
        SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
        mocks.ReplayAll();

My question is I have seen the above being use in using statements e.g

 using (mocks.Record()) { // code here }
 using (mocks.Playback()) { // code here }

What is the purpose of this and difference to what I've done?

A: 

The Record block is used to record expectations, so what comes before the ReplayAll.

The Playback block is actually calling the test, so what comes after the ReplayAll.

You can read more about it here: link text

Kurt
Thanks for the reply, but sorry I'm still confused, my code above for example will return a list for the GetLoanExtended call, so surely I can do an Assert.IsTrue() statement to check what returns is what is expected? I can't see the reason of putting all the above into record/playback statements when its doing the same thing?
David
+1  A: 

These are just another syntax to do the same thing. The following are equivalent:

MockRepository mocks = new MockRepository();
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
mocks.ReplayAll();
//test execution

and:

MockRepository mocks = new MockRepository();
using (mocks.Record()) {
    ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
    SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
}
using (mocks.Playback()) {
    //test execution
}

To make things even more complicated there is a new, third syntax where you don't have explicit record and playback phases called Arrange, Act, Assert Syntax, see e.g. http://ayende.com/blog/archive/2008/05/16/rhino-mocks--arrange-act-assert-syntax.aspx

Grzenio
Thanks, so same thing, different syntax.
David