I am using RhinoMocks for my unit testing using the "AAA" syntax and I simply need to assert that a property was updated on one of my mock objects. I've done this many times before using the AAA syntax and this works wonderfully, but this mock object is a bit more complex and that's causing me a problem.
Okay, my code is loosely coupled and I retrieve my Mock object from an IOC. The IOC returns the mock object as a concrete type that supports the interface IMain (I'm using dummy names here). The property that is updated isn't on the IMain interface, but on the IOther interface.
So, to create my mock object with two interfaces I can't use the Static method, but the instance method:
var myMock = new MockRepository().StrictMultiMock<IMain>(typeof(IOther));
All well and good, and stepping through the code I see that the Mock object is used and its property "Thing" is set to be a new instance of "ThingClass".
However, when I attempt to Assert this using:
var myMock = new MockRepository().StrictMultiMock<IMain>(typeof(IOther));
.... (run my code) ...
(myMock as IOther).AssertWasCalled(m => p.Thing = new ThingClass(), options => options.IgnoreArguments());
When I run this my Assert reports:
System.InvalidOperationException : Cannot assert on an object that is not in replay mode. Did you forget to call ReplayAll() ?
However, I don't have a ReplayAll() method, just a Replay(). When I add it as shown below (either with the cast to IOther or without):
var myMock = new MockRepository().StrictMultiMock<IMain>(typeof(IOther));
(myMock as IOther).Replay();
.... (run my code) ...
(myMock as IOther).AssertWasCalled(m => p.Thing = new ThingClass(), options => options.IgnoreArguments());
Then when my code runs and it attempts to update the property, then it crashes saying:
IOther.set_Thing(ThingClass); Expected #0, Actual #1.
Not sure what it's expecting me to do here.
Any help much appreciated.
Thanks
Griff
The problem was that with
AssertWasCalled(m => p.Thing = new ThingClass()
you weren't setting expectations or stubs on your strict mock. That's why it crashed when Thing property was set.
I don't think you need .Repeat.Once() on your expectation - it's the default option.