如何使用 Rhino Mocks 在 PartialMock 上模拟属性设置器

发布于 2024-08-31 21:24:03 字数 309 浏览 2 评论 0原文

我想防止在部分类的属性上调用真正的设置器代码。

这个的语法是什么?

我当前的代码用于消除getter(我也想消除setter):

var user = MockRepository.GeneratePartialMock<User>(ctor params...);
user.MyProperty = "blah";

类似这样的东西吗?

user.Stub(u => u.MyProperty).Do(null);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2024-09-07 21:24:03

这是一个 3.5 示例,可以满足您的需要(我认为您上面的语法是 3.1 或 3.2)。

首先,我有一个用于属性设置器调用的委托:

private delegate void NoAction(string value);

然后除了“Do”之外,还使用带有“SetPropertyAndIgnoreArgument”的 Expect.Call:

var repository = new MockRepository();
var sample = repository.PartialMock<Sample>();

Expect.Call(sample.MyProperty).SetPropertyAndIgnoreArgument().Do(new NoAction(DoNothing));
sample.Replay();

sample.DoSomething();

repository.VerifyAll();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文