问题
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 current 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);
回答1:
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();
来源:https://stackoverflow.com/questions/2771665/how-to-mock-a-property-setter-on-a-partialmock-using-rhino-mocks