2

有没有办法用 params 关键字来摩尔/存根/模拟方法?

下面是我试图摩尔/存根的方法的一个例子:

Void SomeMethod(bool finalizer,params string[] parameters)...

我试图像这样摩尔它:

scriptServiceStub.SomeMethodBooleanStringArray=
                (bool finalizer, params string[] parameters) =>
                    {
                        agentCommCalled = true;
                    };

我收到以下编译错误:

'; 预期”和“预期类型”

突出显示 params 关键字。

4

2 回答 2

1

我通过在测试类文件中创建方法并将存根分配给这个新方法找到了解决方法。

[TestMethod()] 
[HostType("Moles")] 
public void NotifyAgentTest() 
{ 
    ... 
    //ensure that the correct values are passed to the agentComm. 
    // have to use a real method because of the parameters param w/ the params keyword. 
    scriptServiceStub.AgentCommStringArray = AgentCommDelegate; 
    ... 
    Assert.IsTrue(agentCommCalled);

}

public bool AgentCommDelegate(bool finalizer, params string[] parameters) 
{
    agentCommCalled = true; return true; 
}
于 2011-06-23T16:40:19.563 回答
1

只需从您的示例中的委托签名中删除 params 关键字,它就可以正常工作。

scriptServiceStub.SomeMethodBooleanStringArray=
            (bool finalizer, string[] parameters) =>
                {
                    agentCommCalled = true;
                };
于 2011-11-22T21:58:38.757 回答