我是否应该使用 AutoFixture 来测试我的洋葱的核心元素,它没有依赖关系?

Should I be using AutoFixture to test the Core element of my Onion, which has no dependancies?

这个问题是我之前在此处提出的问题的后续问题:How to use OneTimeSetup? 特别是一位回答者的回答。请看下面的代码:

[TestFixture]
public class MyFixture
{
    IProduct Product;

    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
        IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
        Product = fixture.Create<Product>();
    }
    //tests to follow
}

AutoMoq 是否仅用于创建模拟?我问的原因是因为我早些时候在这里阅读了一个问题,回答者暗示它也用于创建普通类型,即不是 Mocks。

我想测试我的 Onion 的核心元素,它没有依赖项。因此我应该使用 AutoFixture 吗?

AutoMoq 胶水库为 AutoFixture 提供了 Auto-Mocking Container 的附加功能;也就是说,它不仅可以为您组合普通对象——如果需要的话,它还可以为您的对象提供模拟对象。

AutoFixture 本身不是一个 Auto-Mocking 容器,而是一个自动化 Fixture Setup 阶段 Four Phase Test 的库模式,如 xUnit Test Patterns. It also enables you to automate code associated with the Test Data Builder 模式中所述。

它被明确设计为 隐式设置 的替代方案,所以我认为将它与设置方法和可变 class 字段一起使用没有什么意义这个问题表明。换句话说,AutoFixture 的全部意义在于它使您能够像这样编写独立的单元测试:

[TestFixture]
public class MyFixture
{
    [Test]
    public void MyTest()
    {
        var fixture = new Fixture().Customize(new AutoMoqCustomization());
        var product = fixture.Create<Product>();

        // Use product, and whatever else fixture creates, in the test
    }
}

你绝对可以用它来测试你的单元,即使它们没有依赖关系,但在那种情况下,你可能不需要 AutoMoq 自定义。