我有一个单元测试,必须模拟返回布尔类型的非虚拟方法
public class XmlCupboardAccess
{
public bool IsDataEntityInXmlCupboard(string dataId,
out string nameInCupboard,
out string refTypeInCupboard,
string nameTemplate = null)
{
return IsDataEntityInXmlCupboard(_theDb, dataId, out nameInCupboard, out refTypeInCupboard, nameTemplate);
}
}
所以我有一个XmlCupboardAccess
类的模拟对象,我正在尝试在测试用例中为此方法设置模拟,如下所示
[TestMethod]
Public void Test()
{
private string temp1;
private string temp2;
private Mock<XmlCupboardAccess> _xmlCupboardAccess = new Mock<XmlCupboardAccess>();
_xmlCupboardAccess.Setup(x => x.IsDataEntityInXmlCupboard(It.IsAny<string>(), out temp1, out temp2, It.IsAny<string>())).Returns(false);
//exception is thrown by this line of code
}
但是这行抛出异常
Invalid setup on a non-virtual (overridable in VB) member:
x => x.IsDataEntityInXmlCupboard(It.IsAny<String>(), .temp1, .temp2,
It.IsAny<String>())
任何建议如何解决这个异常?
它很简单..您需要标记它
—
西蒙·怀特海德
virtual
。Moq无法模拟无法覆盖的具体类型。
XmlCupboardAccess
什么?