Question

I'm trying to assert with RhinoMocks that a certain property setter was called. But it's not working as expected.

The following simplified example illustrates the problem.

Consider this interface:

public interface IMyInterface
{
    string SomeProperty { get; set; }
}

And now consider the following code:

var mock = MockRepository.GenerateStub<IMyInterface>();
mock.SomeProperty = "abc";

mock.AssertWasCalled(x => x.SomeProperty = Arg<string>.Is.Anything);

I was expecting the assert on the last line would pass without problem. However, it is throwing an ExpectationViolationException with this message:

"IMyInterface.set_SomeProperty(anything); Expected #1, Actual #0."

I can't understand why this should happen. Can anyone please help?

Was it helpful?

Solution

The object returned by GenerateStub<T> doesn't record property and method calls. If you want to assert if setters, getters, or methods have been called, use GenerateMock<T> instead.

// Replace
var mock = MockRepository.GenerateStub<IMyInterface>();

// with
var mock = MockRepository.GenerateMock<IMyInterface>();

// and everything should work again.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top