San*_*osh 23 c# rhino-mocks properties stub
使用RhinoMocks,我试图Stub一个属性的getter值.该属性被定义为仅具有getter访问权限的接口的一部分.
但是我收到错误"无效的呼叫,已使用最后一次呼叫或未进行任何呼叫(确保您正在调用虚拟(C#)/可覆盖(VB)方法)." 我明白这可能意味着我存在的财产不是虚拟的; 但它是接口的一部分,我不确定这是否是我收到此错误的原因..
下面是代码框架.如果我取消注释"stubRepository.Stub(x => x.StoreDeviceID).PropertyBehavior();"的行,那么我得到一个新错误"属性必须是读/写".我搜索了SO并找到了这个页面.但建议的解决方案对我没有帮助.有什么想法吗?
public interface IStore {
string StoreDeviceID {get;}
//other methods
}
public static class Store {
private IStore Repository;
public void SetRepository(IStore rep){
Repository = rep;
}
public StoredeviceID {
get{
return Repository.StoreDeviceID;
}
}
//other methods
}
public class TestClass {
[Test]
public void TestDeviceID() {
var stubRepository =
MockRepository.GenerateStub<IStore>();
Store.SetRepository(stubRepository);
//stubRepository.Stub(x => x.StoreDeviceID).PropertyBehavior();
SetupResult.For(stubRepository.StoreDeviceID).Return("test");
Assert.AreSame(Store.StoreDeviceID, "test");
}
}
Run Code Online (Sandbox Code Playgroud)
Ada*_*kis 34
由于这是一个只读属性,您需要说:
stubRepository.Stub(x => x.StoreDeviceID).Return("test");
Run Code Online (Sandbox Code Playgroud)
通常使用存根,使用属性,就像普通的C#属性一样.因此,对于非只读属性,您会说:stubRepository.someProperty = "test";
另请注意,如果您想设置一种方法以某种方式运行,无论它是模拟还是存根,您总是会说:
stubRepository.Stub(x => x.someMethod()).Return("foo");
Run Code Online (Sandbox Code Playgroud)
请记住,存根可以为您的单元测试提供所需的依赖项,但是不能运行验证; 这就是嘲笑的目的.
如果要提供以某种方式运行的依赖项,请使用存根.如果要验证某个依赖项是否已正确交互,请使用模拟.
来自(杰出的)Rhino Wiki:
模拟是一个我们可以设置期望的对象,它将验证预期的操作确实已经发生.存根是您用于传递给测试代码的对象.您可以设置它的期望,因此它会以某种方式起作用,但这些期望永远不会得到验证.存根的属性将自动表现为普通属性,您无法设置它们的期望.
如果要验证测试代码的行为,您将使用具有适当期望的模拟,并验证.如果您只想传递可能需要以某种方式执行的值,但不是此测试的焦点,则将使用存根.
重要信息:存根永远不会导致测试失败.
| 归档时间: |
|
| 查看次数: |
21276 次 |
| 最近记录: |