嘲諷私有字段我有以下的類定義,由此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();
}