c# - FakeItEasy 不允许设置返回值

标签 c# unit-testing fakeiteasy

我不明白为什么 FakeItEasy 不允许我为带有参数的公共(public)方法设置返回值。

代码:

var fakeInstanse = A.Fake<SomeClass>();
A.CallTo(() => fakeInstanse.Method(param1, param1));

方法 是公共(public)方法,接受两个参数。通常我会在代码的第二行调用 Returns() 方法,但 Visual Studio 不会在可用代码中显示它。

什么可能会影响此行为? SomeClassMethod 定义的哪一部分可能导致此问题?

最佳答案

一般来说,这应该有效。看看这个通过的测试:

public class SomeClass
{
    public virtual int Method(int arg1, int arg2)
    {
        return 7;
    }
}

[TestFixture]
public class TestFixture
{
    [Test]
    public void Should_be_able_to_set_return_value()
    {
        const int param1 = 9;
        var fakeInstanse = A.Fake<SomeClass>();
        A.CallTo(() => fakeInstanse.Method(param1, param1))
            .Returns(8);
        Assert.That(fakeInstanse.Method(param1, param1), Is.EqualTo(8));
    }
}

方法 的返回类型是什么?根据您的描述,我猜它是 void

您能向我们展示 SomeClass(和 SomeClass.Method)的声明吗?否则,我们将无法给出建设性的答案。此外,您可以在 the FakeItEasy "what can be faked" documentation page 找到一些帮助。 .

关于c# - FakeItEasy 不允许设置返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23267993/

相关文章:

c# - 为什么 C# 中的 Selenium 没有 'WebElement' 类?

python - 使用 patch decorator 和 side_effect 模拟文件读取

java - 模拟 void 方法,改变它们的参数

c# - FakeItEasy Proxy 方法调用真实实现

c# - 在 C# 中使用 FakeItEasy 制作假 MongoDB 集合

c# - 如何处理 Redis 哈希的强类型?

c# - MVC3 REST 路由和 Http 动词

c# - 完全运行时单元测试失败 - API 限制 : The assembly has already loaded from a different location

c# - 在每个项目的基础上同步线程

c# - Async TestInitialize 保证测试失败