AutoFixture未能CreateAnonymous MVC控制器 [英] AutoFixture fails to CreateAnonymous MVC Controller

查看:214
本文介绍了AutoFixture未能CreateAnonymous MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code:

IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.Customize<ViewDataDictionary>(c => c.Without(x => x.ModelMetadata));
var target = fixture.CreateAnonymous<MyController>();

异常:

System.Reflection.TargetInvocationException:   System.Reflection.TargetInvocationException:异常被抛出   通过调用的目标。 ---> System.NotImplementedException:   该方法或操作未实现

System.Reflection.TargetInvocationException: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotImplementedException: The method or operation is not implemented.

myController的()需要3个参数。

我试着在这里答案描述的修补程序,但它不会工作。

I've tried the fix described in the answer here but it wouldn't work.

推荐答案

<一个href="http://stackoverflow.com/questions/13869968/how-do-i-apply-autofixture-customizations-to-anything-inheriting-from-a-base-cla#comment19120515_13875306">As似乎,使用MVC 4时,你必须用不同的方式自定义灯具实例。

As it seems, when using MVC 4 you have to customize the Fixture instance in a different way.

测试应该通过,如果你的替换

The test should pass if you replace:

fixture.Customize<ViewDataDictionary>(c => c
    .Without(x => x.ModelMetadata));

fixture.Customize<ControllerContext>(c => c
    .Without(x => x.DisplayMode));


另外,您可以创建所需的自定义内容的复合

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

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

然后,将原来的测试可以被改写为:

Then, the original test could be rewritten as:

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

    var sut = fixture.CreateAnonymous<MyController>();

    Assert.IsAssignableFrom<IController>(sut);
}

这篇关于AutoFixture未能CreateAnonymous MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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