【发布时间】:2011-10-23 11:37:04
【问题描述】:
使用 RhinoMocks,我试图对属性的 getter 值进行 Stub。该属性被定义为仅具有 getter 访问权限的接口的一部分。
但是我收到错误“无效调用,已使用最后一次调用或未进行调用(确保您正在调用虚拟 (C#) / 可覆盖 (VB) 方法)。”我理解这可能意味着我存根的属性不是虚拟的;但是它是接口的一部分,我不确定这是否是我收到此错误的原因..
下面是代码框架。如果我取消注释“stubRepository.Stub(x => x.StoreDeviceID).PropertyBehavior();”的行,那么我会收到一个新错误“属性必须是读/写”。我在 SO 上搜索并找到 this 页面。但是建议的解决方案对我没有帮助。有什么想法吗?
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");
}
}
【问题讨论】:
标签: c# properties rhino-mocks stub