How to mock a property setter on a PartialMock using Rhino Mocks
I'd like to prevent the real setter code being invoked on a property on a partial class.
What is the syntax for this?
My cu开发者_C百科rrent code to stub out the getter (I'd like to also stub out the setter):
var user = MockRepository.GeneratePartialMock<User>(ctor params...);
user.MyProperty = "blah";
Something like this?
user.Stub(u => u.MyProperty).Do(null);
Here's a 3.5 sample that does what you need (I think your syntax above is 3.1 or 3.2).
First, I have a delegate for the property setter call:
private delegate void NoAction(string value);
Then use the Expect.Call with "SetPropertyAndIgnoreArgument" in addition to the "Do":
var repository = new MockRepository();
var sample = repository.PartialMock<Sample>();
Expect.Call(sample.MyProperty).SetPropertyAndIgnoreArgument().Do(new NoAction(DoNothing));
sample.Replay();
sample.DoSomething();
repository.VerifyAll();
精彩评论