我该如何申请AutoFixture自定义任何东西从基类继承? [英] How do I apply AutoFixture customizations to anything inheriting from a base class?

查看:216
本文介绍了我该如何申请AutoFixture自定义任何东西从基类继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试干起来我的单元测试,我试图用AutoFixture作为IoC容器来实例化我的测试(SUT)系统,在这种特殊情况下是ASP.NET MVC 控制器秒。因此,我想自定义AutoFixture创建无自动性能控制器。

In an attempt to DRY up my unit tests, I'm trying to use AutoFixture as an IoC container to instantiate my system under test (SUT), which in this particular case are ASP.NET MVC Controllers. Therefore, I want to customize AutoFixture to create controllers without auto-properties.

我尝试添加了 ControllerBase 自定义,但它似乎并不为 ControllerBase 的子类的工作。

I tried adding a customization for ControllerBase, but it doesn't seem to work for subclasses of ControllerBase.

fixture.Customize<ControllerBase>(c => c.OmitAutoProperties());

下面是我希望能够编写测试的例子:

Here's an example of the test I want to be able to write:

    [Theory, AutoFixtureData]
    public void ControllerTest(AccountController controller) {
        Assert.Equal(default(UrlHelper), controller.Url);
    }



当然,它的工作原理,如果我手动添加一个自定义在每个特定的控制器我项目,但谁愿意这样做呢?有没有更好的办法?

Naturally, it works if I manually add one customization for each specific controller in my project, but who wants to do that? Is there a better way?

推荐答案

以下的定制通过上面的测试,并执行你似乎要问什么

The following Customization passes the above test, and does what you seem to ask

public class MvcCostumization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(
            new FilteringSpecimenBuilder(
                new MethodInvoker(new ModestConstructorQuery()),
                new ControllerSpecification()));
    }

    private class ControllerSpecification : IRequestSpecification
    {
        public bool IsSatisfiedBy(object request)
        {
            var t = request as Type;
            if (t == null)
                return false;

            return typeof(ControllerBase).IsAssignableFrom(t);
        }
    }
}



不过,禁用的所有的所有MVC控制器,包括用户属性的HttpContext 的ModelState 请求响应等等。你可能需要在这些单元测试,所以都你确定要这么做?

However, that disables all properties of all MVC Controllers, including User, HttpContext, ModelState, Request, Response, etc. You might need those in unit tests, so are you sure you want to do that?

我会的第一人同意MVC 控制器基地类一个巨大的SRP违规,但它是给我们一个班,所以我们没有什么可以做,除了努力做到最好吧。

I'll be the first person to agree that the MVC Controller base class is one huge SRP violation, but it's a class which is given to us, so there's nothing we can do about except trying to make the best of it.

一般情况下,你只需要扭动它一点使AutoFixture填补大部分属性自动

Usually, you only need to twist it a little to make AutoFixture fill most of the properties automatically.

在MVC 3项目,为此,结合的AutoMoq扩展,就足以创造Controller实例:

In MVC 3 projects, this, combined with the AutoMoq extension, is sufficient to create Controller instances:

fixture.Customize<ModelBindingContext>(c => c
    .Without(x => x.Model)
    .Without(x => x.ModelType));
fixture.Inject(CultureInfo.CurrentCulture);

这篇关于我该如何申请AutoFixture自定义任何东西从基类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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