单元测试的HTML帮助与AutoFixture [英] Unit Testing an Html Helper with AutoFixture

查看:196
本文介绍了单元测试的HTML帮助与AutoFixture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单元测试的HTML辅助使用AutoFixture。下面是我的SUT

I'm attempting to Unit Test an Html Helper using AutoFixture. Below is my SUT

public static MvcHtmlString SampleTable(this HtmlHelper helper,
    SampleModel model, IDictionary<string, object> htmlAttributes)
{
    if (helper == null)
    {
        throw new ArgumentNullException("helper");
    }
    if (model == null)
    {
        throw new ArgumentNullException("model");
    }

    TagBuilder tagBuilder = new TagBuilder("table");
    tagBuilder.MergeAttributes(htmlAttributes);
    tagBuilder.GenerateId(helper.ViewContext.HttpContext.Items[Keys.SomeKey].ToString());
    return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
}



正如你可以看到它会返回一个MVC HTML字符串与表的标签和标识连接到它。 (请参见下面的单元测试结果的例子)

As you can see it just returns an MVC Html string with table tags and Id attached to it. (See below Unit Test result for an example)

单元测试与AutoFixture:

Unit Test with AutoFixture:

[Fact]
public void SampleTableHtmlHelper_WhenKeyExistWithinHttpContext_ReturnsExpectedHtml()
{
    var fixture = new Fixture();

    //Arrange
    fixture.Inject<HttpContextBase>(new FakeHttpContext());
    var httpContext = fixture.CreateAnonymous<HttpContextBase>();
    fixture.Inject<ViewContext>(new ViewContext());
    var vc = fixture.CreateAnonymous<ViewContext>();

    vc.HttpContext = httpContext;
    vc.HttpContext.Items.Add(Keys.SomeKey, "foo");

    fixture.Inject<IViewDataContainer>(new FakeViewDataContainer());
    var htmlHelper = fixture.CreateAnonymous<HtmlHelper>();
    var sampleModel = fixture.CreateAnonymous<SampleModel>();

    //Act
    var result = SampleHelpers.SampleTable(htmlHelper, sampleModel, null).ToString();

    //Assert
    Assert.Equal("<table id=\"foo\"></table>", result);
}      



FakeHttpContext和FakeViewDataContainer只是HttpContextBase和IViewDataContainer的假冒实现。

FakeHttpContext and FakeViewDataContainer are just the fake implementations of HttpContextBase and IViewDataContainer.

本测试通过,并返回预期的结果。但是,我不知道我在正确利用Autofixture在这里。有没有这个单元测试中使用AutoFixture更好的办法?

This test passes and returns the expected result. However, I ‘m not sure I’m correctly utilizing the Autofixture here. Is there a better way to use AutoFixture within this Unit Test?

推荐答案

根据部分信息是很难说究竟是怎么以上试验可以进一步减少,但我猜想,这可能会减少。

Based on partial information it's hard to tell exactly how the above test could be further reduced, but I would guess that it could be reduced.

首先,调用的组合注入然后按 CreateAnonymous 是相当地道的 - 特别是如果你颠倒顺序。这被称为冻结的匿名值(相当于DI容器的单件的寿命范围内)。它可以更简洁地说是这样的:

First of all, the combo of invoking Inject followed by CreateAnonymous is rather idiomatic - particularly if you reverse the sequence. This is called Freezing the anonymous value (and is equivalent to a DI container's Singleton lifetime scope). It can be stated more succinctly like this:

var vc = fixture.Freeze<ViewContext>();



它也好像在测试映射的HttpContext FakeHttpContext。 映射可以做稍微容易一些,但会映射瞬态实例...

It also seems as though the test is mapping HttpContext to FakeHttpContext. Mapping can be done a little bit easier, but that'll map Transient instances...

在任何情况下,除非有令人信服的理由使用的手动嘲笑,而不是一个动态模拟库,你还不如决定使用 AutoFixture作为自动嘲讽容器。这可能你摆脱了很多类型映射的。

In any case, unless you have compelling reasons to use Manual Mocks instead of a dynamic Mock library, you might as well decide to use AutoFixture as an auto-mocking container. That might rid you of a lot of that type mapping.

因此,考虑到这一切,我的的,你也许能减少测试是这样的:

So, given all that, I'd guess that you might be able to reduce the test to something like this:

[Fact]
public void SampleTableHtmlHelper_WhenKeyExistWithinHttpContext_ReturnsExpectedHtml()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());

    //Arrange
    var vc = fixture.Freeze<ViewContext>();
    vc.HttpContext.Items.Add(Keys.SomeKey, "foo");

    var htmlHelper = fixture.CreateAnonymous<HtmlHelper>();
    var sampleModel = fixture.CreateAnonymous<SampleModel>();

    //Act
    var result = SampleHelpers.SampleTable(htmlHelper, sampleModel, null).ToString();

    //Assert
    Assert.Equal("<table id=\"foo\"></table>", result);
}



然而,大多数的安排一部分现在是纯声明,并因为你似乎已经是使用xUnit.net,您可以使用 AUTODATA理论为AutoFixture 的移动大部分变量方法的参数:

However, most of the Arrange part is now purely declarative, and since you seem to already be using xUnit.net, you can use AutoData Theories for AutoFixture to move most of the variables to method arguments:

[Theory, AutoMoqData]
public void SampleTableHtmlHelper_WhenKeyExistWithinHttpContext_ReturnsExpectedHtml(
    [Frozen]ViewContext vc,
    HtmlHelper htmlHelper,
    SampleModel sampleModel)
{
    //Arrange
    vc.HttpContext.Items.Add(Keys.SomeKey, "foo");

    //Act
    var result = SampleHelpers.SampleTable(htmlHelper, sampleModel, null).ToString();

    //Assert
    Assert.Equal("<table id=\"foo\"></table>", result);
}

这假设您已经桥接AutoMoqCustomization与AutoDataAttribute这样的:

This assumes that you've bridged the AutoMoqCustomization with the AutoDataAttribute like this:

public class AutoMoqDataAttribute : AutoDataAttribute
{
    public AutoMoqDataAttribute :
        base(new Fixture().Customize(new AutoMoqCustomization()))
    { }
}

请请记住,你可能需要调整上面的代码位,使其适合您的API的细节。这只是意味着作为一个草图。

Please keep in mind that you may need to tweak the above code a bit to make it fit the details of your API. This is only meant as a sketch.

这篇关于单元测试的HTML帮助与AutoFixture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆