【发布时间】:2011-01-12 18:46:39
【问题描述】:
我有以下类定义,其中 attribute 字段通过 NHibernate 的反射进行水合。该字段没有作为对象公开,而是我想隐藏它的实现,只提供引用 attribute 字段属性的属性。
public class CustomerAttribute : ICustomerAttribute
{
private IAttribute attribute;
public string DisplayName
{
get { return attribute.DisplayName;}
}
}
我正在尝试使用 RhinoMocks 模拟此对象,但我不确定如何为 attribute 字段添加水分以进行测试。我尝试通过反射手动设置attribute 字段,但我从RhinoMocks 收到代理错误(这是有道理的)。
那么我如何为attribute 字段添加水分以测试 CustomerAttribute 对象的属性?
这是我现在的测试...
[Test]
public void PropertiesTest()
{
MockRepository mock = new MockRepository();
ICustomerAttribute attribute = mock.StrictMock<ICustomerAttribute>();
//Set the attribute field
FieldInfo fieldInfo = typeof(CustomerAttribute).GetField("attribute",
BindingFlags.Instance | BindingFlags.SetField |
BindingFlags.NonPublic);
fieldInfo.SetValue(attribute, new Domain.Attribute()); //This does not work
Expect.Call(attribute.DisplayName).Return("Postal Code");
mock.ReplayAll();
Assert.AreEqual(true, attribute.DisplayName);
mock.VerifyAll();
}
【问题讨论】:
标签: rhino-mocks field private