简体   繁体   English

无法分配给 .. 因为它是一个组方法

[英]Cannot assign to .. because it is a group method

I am using Moq to write test cases and when I try to assign the mock function within the constructor it comes up with the error saying that I cannot assign it because the target method is a group method however there is only one method defined.我正在使用 Moq 编写测试用例,当我尝试在构造函数中分配模拟函数时,它出现错误,说我无法分配它,因为目标方法是一个组方法,但是只定义了一个方法。

I have written an interface etc. as follows:我写了一个接口等如下:

... ...

public interface IRSPPortal
{
    string GetOrderStatus(OrderInfo OrderNum);
}

public class RSPPortal : IRSPPortal
{

    public RSPPortal(IRSPPortal GetOrderStatusMock)
    {
        this.GetOrderStatus = GetOrderStatusMock; //This line gives the error
    }

    public string GetOrderStatus(OrderInfo OrderNum)
    {
         //stuff done here to access a database
    }
}

... ...

There is only one method GetOrderStatus(OrderInfo OrderNum) so I don't know why it has classified it as a method group.只有一个方法 GetOrderStatus(OrderInfo OrderNum) 所以我不知道为什么它把它归类为一个方法组。 What is the simplest way to overcome this error?克服此错误的最简单方法是什么?

I have tried to use this.GetOrderStatus = GetOrderStatusMock.GetOrderStatus;我曾尝试使用 this.GetOrderStatus = GetOrderStatusMock.GetOrderStatus; but that didn't work either.但这也不起作用。

Thanks everyone.谢谢大家。

That's not how Moq works.这不是 Moq 的工作方式。 If you want to mock IRSPPortal:如果你想模拟 IRSPPortal:

Mock<IRSPPortal> rspPortalMock = new Mock<IRSPPortal>();

rspPortalMock.Setup(a => a.GetOrderStatus(It.IsAny<OrderInfo>())).Returns(<string you want returned from the mocked method>);

Now you have a mock of IRSPPortal that will return whatever string you want from GetOrderStatus.现在您有一个 IRSPPortal 的模拟,它将从 GetOrderStatus 返回您想要的任何字符串。

To use that mocked object just pass in rspPortalMock.Object to whatever class is using IRSPPortal.要使用该模拟对象,只需将 rspPortalMock.Object 传递给使用 IRSPPortal 的任何类。

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

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