【问题标题】:FakeItEasy does not allow to setup value to returnFakeItEasy 不允许设置返回值
【发布时间】:2014-04-24 11:46:55
【问题描述】:

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

代码:

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

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

什么可能会影响这种行为? SomeClassMethod 定义的哪一部分可能导致这种情况?

【问题讨论】:

    标签: c# unit-testing fakeiteasy


    【解决方案1】:

    一般来说,这应该可以。看看这个通过测试:

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

    Method 的返回类型是什么?根据你的描述,我猜是void

    你能告诉我们SomeClass(和SomeClass.Method)的声明吗?否则,我们将无法给出建设性的答案。此外,您还可以通过the FakeItEasy "what can be faked" documentation page 获得帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      相关资源
      最近更新 更多