繁体   English   中英

用Moq测试私有方法不起作用

[英]Testing private method with Moq doesn't work

我正在使用起订量,对它来说是新手。 我需要测试一个私有方法。

我有2个程序集:

CustomerTest.dll
CustomerBusiness.dll

因此,CustomerTest dll具有如下类:

[TestFixture]
public class CustomerTestFixture
{
    var customerMock=new Mock<ICustomer>()
    customerMock.Protected().Setup<bool>("CanTestPrivateMethod").Returns(true);

   etc...
}

CustomerBusiness.dll具有

公共接口ICustomer {void Buy(); }

public class Customer:ICustomer
{
    public void Buy()
    {
        etc...
    }

    protected virtual bool CanTestPrivateMethod()
    {
        return true;
    }

}

我收到以下错误

System.ArgumentException : Member ICustomer.CannotTestMethod does not exist.
at Moq.Protected.ProtectedMock`1.ThrowIfMemberMissing(String memberName, MethodInfo method, PropertyInfo property)
at Moq.Protected.ProtectedMock`1.Setup(String methodOrPropertyName, Object[] args)

我还添加了[assembly: InternalsVisibleTo("CustomerTest.CustomerTestFixture")但没有区别!

我究竟做错了什么。 我知道我的接口没有这样的方法,这就是要点,因为我的方法必须是私有的。 你能帮个例子吗?

创建模拟对象时,它会实现要模拟的类型或接口的虚拟和抽象成员。

通过创建Mock<ICustomer> ,它实现了客户界面的一种方法。 它不了解您在Customer类中添加的方法。 如果需要模拟该方法,则需要创建一个Mock<Customer>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM