我有一些方法使用反射將一種對象類型轉換爲另一種。我正在通過Moq測試轉換方法,並且偶然發現了一個我不知道如何處理的行爲。當我通過Moq對象反射來獲取PropertyInfo時,我得到兩個額外的對象。在Moq對象上反射產生2個附加屬性
Moq.Mock``1[Namespace.Class+IElement] Mock
Moq.Mock Mock
重現這個代碼如下:
public void Moq_Reflection() {
var realElement = new Stuff();
// Produces 2 items
PropertyInfo[] pInfo = realElement.GetType().GetProperties();
var mockElement = new Mock<IElement>();
mockElement.Setup(e => e.Property1).Returns(12);
mockElement.Setup(e => e.Property2).Returns(42);
// Produces 4 items
pInfo = mockElement.Object.GetType().GetProperties();
}
public interface IElement {
int Property1 { get; set; }
int Property2 { get; set; }
}
public class Stuff : IElement
{
public int Property1
{
get { return -1; }
set { }
}
public int Property2
{
get { return -2; }
set { }
}
}
有沒有一種方法,以反映起訂量的對象上,而不是檢索這些屬性?
我一直希望它不會那樣。沒有什麼比在您的主應用程序中編寫代碼來補償您的測試更醜陋的了。謝謝,但我很感激幫助! – 2009-07-20 18:11:10