tags:

views:

39

answers:

1

I was having problems with a second call on a mock in my test run, so I moved the double calls into the test method. I have this:

RefBundle mockIRefBundle = mocks.StrictMock<IRefBundle>();

Expect.Call(mockIRefBundle.MaxTrackItems).Return( 6 ).Repeat.Any();

int q = mockIRefBundle.MaxTrackItems;
int z = mockIRefBundle.MaxTrackItems;

It fails when I make the second call to set "z" with an exception that implies the method was already called:

Error message:

System.InvalidOperationException: Previous method 
'IRefBundle.get_MaxTrackItems();
'requires a return value or an exception to throw..

and Stack

Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation invocation, 
...

The second call doesn't seem to honor the Repeat.Any()

What am I missing?

+1  A: 

Either you have to use the new syntax:

RefBundle mockIRefBundle = MockRepository.GenerateMock<IRefBundle>();

mockIRefBundle.Expect(X => x.MaxTrackItems).Return( 6 ).Repeat.Any();

int q = mockIRefBundle.MaxTrackItems;
int z = mockIRefBundle.MaxTrackItems;

or alternatively you need to call ReplayAll() before you start using your mocks:

RefBundle mockIRefBundle = MockRepository.GenerateMock<IRefBundle>();

mockIRefBundle.Expect(X => x.MaxTrackItems).Return( 6 ).Repeat.Any();

mocks.ReplyAll();
int q = mockIRefBundle.MaxTrackItems;
int z = mockIRefBundle.MaxTrackItems;
Grzenio
Thanks - I find the Rhino docs to be very confusing, so I end up mixing and matching syntax. Can you recommend a good guide?
ddm
Hi @ddm, I don't really know any good guides myself :( Lots of practice + stackoverflow I would say.
Grzenio
Thanks @Grzenio - what would we do without SO :-)
ddm