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

发布于 2024-11-16 12:21:10 字数 498 浏览 3 评论 0原文

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

这是我尝试摩尔/存根的方法的示例:

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

我尝试像这样摩尔它:

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

我收到以下编译错误:

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

突出显示 params 关键字。

Is there a way to mole/stub/mock a method with the params keyword?

Here is an example of the Method I am trying to mole/stub:

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

I have tried to mole it like so:

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

I am getting the following compile error:

'; expected' and 'Type Expected'

that highlights the params keyword.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过潦 2024-11-23 12:21:10

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

[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; 
}

I found a work around by creating method in the test class file and assigning the stub to this new method.

[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; 
}
2024-11-23 12:21:10

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

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

Just remove the params keyword from the delegate signature in your example and it will work just fine.

scriptServiceStub.SomeMethodBooleanStringArray=
            (bool finalizer, string[] parameters) =>
                {
                    agentCommCalled = true;
                };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文