Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub property setter with Rhino.Mocks to execute an action when called?

This can be done with methods as follows:

myStub.Stub(x => x.SomeMethod()).WhenCalled(x => do something);

Is there any way the same thing can be done when a property setter is called?

like image 678
Jay Avatar asked Nov 07 '10 03:11

Jay


3 Answers

The following will work:

Expect.Call(myStub.MyProperty).WhenCalled(p => Console.WriteLine("Called")).Return("Test result");

Please note that this only works if your properties (MyProperty in this case) is declared virtual.

like image 151
Pieter van Ginkel Avatar answered Sep 20 '22 22:09

Pieter van Ginkel


I don't have Visual Studio handy, but I am sure something along these lines should work:

myStub.Stub(x => x.SomeProperty = null).WhenCalled(x => do something);
like image 42
Grzenio Avatar answered Sep 20 '22 22:09

Grzenio


Put the mock back into "record" mode and use the old record/playback semantics to set the expectation for the property setter:

stub.BackToRecord();
Expect.Call(stub.SomeProperty).SetPropertyAndIgnoreArgument().WhenCalled(p => ...do something...);
stub.Replay();
like image 26
PatrickSteele Avatar answered Sep 20 '22 22:09

PatrickSteele