I want to raise an event on a stub object whenever a certain property is set using Rhino Mocks. E.g.
public interface IFoo
{
int CurrentValue { get; set; }
event EventHandler CurrentValueChanged;
}
Setting CurrentValue will raise the CurrentValueChanged event
I have tried myStub.Expect(x => x.CurrentValue).WhenCalled(y => myStub.Raise... which doesn’t work because the property is settable and it says I’m setting expectations on a property which is already defined to use PropertyBehaviour. Also I am aware that this is an abuse of WhenCalled which I’m none too happy about.
What the correct way of achieving this?
You most probably created a stub, not a mock. The only difference is that the stub has property behavior by default.
So the full implementation is something like the following: