在Moles中有没有办法使用params关键字来骚扰/模拟方法?

时间:2011-06-22 22:15:16

标签: params stub moles pex

有没有办法使用params关键字来鼹鼠/存根/模拟方法?

以下是我尝试使用mole / stub的方法示例:

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

我试图像这样骚扰它:

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

我收到以下编译错误:

  

';预期'和'预期类型'

突出显示params关键字。

2 个答案:

答案 0 :(得分: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; 
}

答案 1 :(得分:1)

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

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