本文介绍了如何模拟使用犀牛制品一PartialMock属性setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想prevent真正的二传手code上的属性被调用的一个分部类。

什么是语法呢?

我目前的code存根出的的getter 的(我想也存根出二传手):

  VAR用户= MockRepository.GeneratePartialMock<使用者>(构造函数参数...);
user.MyProperty =嗒嗒;
 

这样的事情?

  user.Stub(U => u.MyProperty)。做(空);
 

解决方案

下面是一个3.5的样品,做你所需要的(我认为上述的语法是3.1或3.2)。

首先,我有一个委托属性setter电话:

 私人委托无效无动作(字符串值);
 

然后使用Expect.Call与SetPropertyAndIgnoreArgument除了做:

  VAR库=新MockRepository();
变种样品= repository.PartialMock<样品>();

。Expect.Call(sample.MyProperty).SetPropertyAndIgnoreArgument()DO(新的无动作(DoNothing));
sample.Replay();

sample.DoSomething();

repository.VerifyAll();
 

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);
解决方案

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

这篇关于如何模拟使用犀牛制品一PartialMock属性setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:23