嘲讽私有字段我有以下的类定义,由此attribute
字段经由反射由NHibernate的水合。现场没有公开为对象,而是我想隐藏它的实现,只是提供参考attribute
领域的性能特性。与RhinoMocks
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();
}