如何用 FakeItEasy 设置多个泛型函数的 return 值?

How to set the return value of multiple generic functions with FakeItEasy?

我正在使用 Autofixture 和 FakeItEasy 为通过 NHibernate 与数据库对话的小任务编写单元测试。我的测试用例还包括在数据库中找不到给定对象的场景,因此我想模拟查询 return null.

现在,我通过以下调用设法实现了这一点:

    var _session = _fixture.Freeze<ISession>();
    A.CallTo(_session).WithReturnType<Foo>().Returns(null);
    A.CallTo(_session).WithReturnType<Bar>().Returns(null);
    // ~10 more of these with just different types defined

但我想知道是否有一种方法可以将其设置为默认行为 - 这样所有使用通用参数对 _session 的调用将默认为 return null

不,我不认为 is.Looks 您现在必须依次配置每种类型。 考虑 proposing it as an enhancement!

更新 我看到你创建了 issue 648 来跟踪这个。谢谢!

这是 AutoFixture 和 FakeItEasy 在功能上重叠 的区域。 FakeItEasy 内置支持创建 dummy objects to use in your tests, and so does AutoFixture. The main difference between the two is that FakeItEasy doesn't come up with test values to assign to those objects, while AutoFixture is all about providing sensible defaults that are good enough for most use cases.

当 AutoFixture 必须为接口或抽象 class 提供实例时,它只是 将创建委托 到配置的模拟库,在本例中为 FakeItEasy。这意味着如果你想改变假对象的创建方式,你需要配置 FakeItEasy,而不是 AutoFixture。

不幸的是,正如@BlairConrad ,FakeItEasy 目前不提供一种方法来禁用特定假对象的虚拟创建。