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

China☆狼群 提交于 2019-12-02 16:40:09

问题


This question follows on from my previous question here: How to use OneTimeSetup? and specifically one of the answerers answer. Please see the code below:

[TestFixture]
public class MyFixture
{
    IProduct Product;

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

Is AutoMoq used to create mocks only? The reason I ask is because I was reading a question on here earlier where the answerer implied that it is also used to create normal types i.e. not Mocks.

I want to test the Core element of my Onion, which has no dependencies. Therefore should I be using AutoFixture?


回答1:


The AutoMoq Glue Library gives AutoFixture the additional capabilities of an Auto-Mocking Container; that is, not only can it compose normal objects for you - it can also supply mock objects to your objects, should they be so required.

AutoFixture itself is not an Auto-Mocking Container, but rather a library that automates the Fixture Setup phase of the Four Phase Test pattern, as described in xUnit Test Patterns. It also enables you to automate code associated with the Test Data Builder pattern.

It was explicitly designed as an alternative to Implicit Setup, so I think it makes little sense to use it together with a setup method and mutable class field as this question suggests. In other words, the whole point of AutoFixture is that it enables you to write independent unit tests like this:

[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
    }
}

You definitely can use it to test your units even when they have no dependencies, but in that case, you probably don't need the AutoMoq Customization.



来源:https://stackoverflow.com/questions/48388861/should-i-be-using-autofixture-to-test-the-core-element-of-my-onion-which-has-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!