How to mock a property setter on a PartialMock using Rhino Mocks

拟墨画扇 提交于 2020-01-06 18:08:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!