AutoFixture 新手,试图了解它,但我看不到它对我有帮助

Eya*_*nik 1 autofixture

目前,我正在使用定制的假对象,这些对象在幕后使用 NSubstitute 创建实际对象,但随着项目的发展,它变得非常难以维护,所以我正在尝试寻找替代方案,我希望 AutoFixture 是适合这项工作的工具。

我阅读了文档,但我很挣扎,因为文档很少甚至没有,而且我阅读了 Mark Seemann 的大部分博客文章,包括 CheatSheet。

我很难掌握的一件事是如何使用带有参数的构造函数创建一个对象,在我的例子中,我需要将参数传递给 CsEmbeddedRazorViewEngine 以及将 HttpRequestBase 传递给 ControllerContext。

我的看法是,我需要创建一个假对象,最后创建一个将它们注入到的自定义对象

我还研究了 NBuilder,在那里传递参数似乎稍微微不足道,但我听说过有关 AutoFixture 的好消息,我想尝试一下。:)

我正在尝试减少假物体的数量,所以这是一个真正的测试,我如何使用 AutoFixture 做同样的事情?

[Theory, 
 InlineData("en-US"), 
 InlineData("en-us"), 
 InlineData("en")]
public void Should_return_the_default_path_of_the_view_for_enUS(string language)
{
    // Arrange
    const string EXPECTED_VIEW_PATH = "~/MyAssemblyName/Views/Home/Index.cshtml";

    CsEmbeddedRazorViewEngine engine = CsEmbeddedRazorViewEngineFactory.Create(ASSEMBLY_NAME, VIEW_PATH, string.Empty);

    string[] userLanguage = { language };

    HttpRequestBase request = FakeHttpRequestFactory.Create(userLanguage);

    ControllerContext controllerContext = FakeControllerContextFactory.Create(request);

    // Act
    ViewEngineResult result = engine.FindPartialView(controllerContext, VIEW_NAME, false);

    // Assert
    RazorView razorView = (RazorView)result.View;

    string actualViewPath = razorView.ViewPath;

    actualViewPath.Should().Be(EXPECTED_VIEW_PATH);
}
Run Code Online (Sandbox Code Playgroud)

PS 我使用 xUnit 作为我的测试框架,使用 NSubstitute 作为我的模拟框架,我应该同时安装 AutoFixture.Xunit 和 AutoFixture.AutoNSubstitute 吗?

更新:在了解了越来越多的信息后,我想它不是适合这项工作的工具,因为我尝试用 AutoFixture 替换我的测试替身工厂,而不是用它设置我的 SUT。

由于奇怪的原因,我认为它正在做与 NBuilder 相同的事情,并且从我看来它们是非常不同的工具。

因此,经过一番思考后,我想我应该将测试双打工厂中的方法更改为对象,然后使用 AutoFixture 创建我的 SUT 并将测试双打注入其中。

Nik*_*nis 5

注意:我没有该CsEmbeddedRazorViewEngine类型和所有其他自定义类型的源代码。

以下是使用 AutoFixture 编写的方法

[Theory]
[InlineAutoWebData("en-US", "about", "~/MyAssemblyName/Views/Home/Index.cshtml")]
[InlineAutoWebData("en-US", "other", "~/MyAssemblyName/Views/Home/Index.cshtml")]
public void Should_return_the_default_path_of_the_view_for_enUS(
    string language, 
    string viewName,
    string expected,
    ControllerContext controllerContext,
    CsEmbeddedRazorViewEngine sut)
{
    var result = sut.FindPartialView(controllerContext, viewName, false);
    var actual = ((RazorView)result.View).ViewPath;

    actual.Should().Be(expected);
}
Run Code Online (Sandbox Code Playgroud)

怎么运行的

它使用 AutoFixture 本身以及 xUnit.net 和 NSubstitute 的粘合库:

PM> Install-Package AutoFixture.Xunit
PM> Install-Package AutoFixture.AutoNSubstitute
Run Code Online (Sandbox Code Playgroud)

实际上,您可以InlineAutoWebData通过 AutoFixture 组合内联值和自动生成的数据值 - 还包括使用 NSubstitute 进行自动模拟。

internal class InlineAutoWebDataAttribute : CompositeDataAttribute
{
    internal InlineAutoWebDataAttribute(params object[] values)
        : base(
            new InlineDataAttribute(values),
            new CompositeDataAttribute(
                new AutoDataAttribute(
                    new Fixture().Customize(
                        new WebModelCustomization()))))
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

评论

您实际上可以将WebModelCustomization上面的自定义替换为AutoNSubstituteCustomization并且它可以工作。

但是,假设您使用的是 ASP.NET MVC 4,则需要使用以下命令自定义Fixture实例:

internal class WebModelCustomization : CompositeCustomization
{
    internal WebModelCustomization()
        : base(
            new MvcCustomization(),
            new AutoNSubstituteCustomization())
    {
    }

    private class MvcCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            fixture.Customize<ControllerContext>(c => c
                .Without(x => x.DisplayMode));

            // Customize the CsEmbeddedRazorViewEngine type here.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

进一步阅读