【问题标题】:How to populate method's return values with AutoFixture如何使用 AutoFixture 填充方法的返回值
【发布时间】:2014-11-12 09:45:45
【问题描述】:

我想以不确定的方式自动生成方法的返回值,即每次调用/测试运行时,我都希望方法返回随机值。目前它总是返回方法调用的默认值:

    public interface IReturn
    {
        bool BoolMethod();
        int IntMethod();
    }

    [Fact]
    public void AllReturnsFromAutofixtureMethodsAreFalse()
    {
        IFixture fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
        IEnumerable<IReturn> theBools = fixture.CreateMany<IReturn>();
        Assert.True(theBools.All(tb => tb.BoolMethod() == false));
        Assert.True(theBools.All(tb => tb.IntMethod() == 0));
    }

在像this 这样的问题中,人们可以找到一种方法来实现类似的属性,但不是方法。有什么想法吗?

【问题讨论】:

  • 对于 Moq,有足够的 AutoConfiguredMoqCustomization。我想你应该看看AutoConfiguredNSubstituteCustomization
  • 就是这样,谢谢!看来我使用了稍微过时的 AutoNSubstitute 集成版本,这就是我找不到它的原因。希望我可以将您的评论标记为答案..

标签: autofixture nsubstitute


【解决方案1】:

我没有使用 AutoFixtureNSubstitute 自定义,但是类似于 Moq 库,似乎 AutoConfiguredNSubstituteCustomization 类应该用于实现您想要的高级 AutoFixture 伪造行为。使用它,您可以从存根方法自动生成结果,还可以创建模拟对象链,将冻结的对象注入链中,等等。

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 2016-04-21
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多