当我不希望它与 NSubstitute 一起调用时,函数被调用 [英] Function is called when I do not expect it to with NSubstitute

查看:25
本文介绍了当我不希望它与 NSubstitute 一起调用时,函数被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置我的模拟来调用函数时,我从 NSubstitute 得到了一个我没有预料到的行为.行为的简化版本是

I am getting a behaviour I didnt expect from NSubstitute when setting up my mocks to call a function. A simplified version of the behavuiour is

[Test]
public void NSubstituteTest()
{
    var mockedFoo = Substitute.For<IFoo>();

    mockedFoo.GenerateString(Arg.Any<string>()).Returns(x => GetValue(x.Args()[0]));
    mockedFoo.GenerateString("0").Returns("hi");


    string result1 = mockedFoo.GenerateString("0");
    string result2 = mockedFoo.GenerateString("1");

    Assert.AreEqual("hi", result1);
    Assert.AreEqual("1", result2);
}

private string GetValue(object val)
{
    string returnValue = val != null ? val.ToString() : "I am null";
    System.Diagnostics.Trace.WriteLine(returnValue);
    return returnValue;
}

测试通过,但我得到输出:01

The test passes but I get the output: 0 1

这表明调用了 mockedFoo.GenerateString("0");实际上导致调用 GetValue() 函数.

This indicates that the call to mockedFoo.GenerateString("0"); actually results in a call to the GetValue() function.

如果我对 Moq 也这样做:

If I do the same with Moq:

[Test]
public void MoqTest()
{
    var mockedFoo = new Mock<IFoo>();

    mockedFoo.Setup(x => x.GenerateString(It.IsAny<string>())).Returns((object s) => GetValue(s));
    mockedFoo.Setup(x => x.GenerateString("0")).Returns("hi");


    string result1 = mockedFoo.Object.GenerateString("0");
    string result2 = mockedFoo.Object.GenerateString("1");

    Assert.AreEqual("hi", result1);
    Assert.AreEqual("1", result2);
}

然后我的测试也通过了,但我得到了结果:1

Then my tests also passes but I get the result: 1

表明该函数未被调用.

这种行为是在某处描述的还是我以错误的方式设置的?

Is this behaviour described somewhere or do I set something up in a wrong way perhaps?

推荐答案

这是 NSubstitute 工作方式的副作用:要获得特定语法,它需要实际调用该方法以获取对该方法的引用.

This is a side-effect of how NSubstitute works: to get that particular syntax it needs to actually call the method to get a reference to that method.

>

Moq 和其他人使用 lambdas 并且可以从那里挑选特定的方法,而无需运行方法本身.(这意味着 NSubstitute 也无法检测或抛出非虚拟方法调用.)

Moq and others use lambdas and can pick the particular method out from there, without the need to run the method itself. (This means NSubstitute also fails to detect or throw on non-virtual method calls.)

下一个版本将针对某些会导致问题的情况提供解决方法(尽管不是理想的方法:您需要在调用中设置一个参数匹配器来设置返回值,以便 NSub 提前知道它不是真正的调用),但必须拦截实际方法调用的基本问题仍然存在.

The next release will have a work-around for some cases where this is causing problems (albeit a non-ideal one: you'll need to have an argument matcher in the call that sets the return so NSub knows in advance it is not a real call), but the fundamental issue of having to intercept actual method calls will remain.

这篇关于当我不希望它与 NSubstitute 一起调用时,函数被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆