AutoFixture 无法创建匿名 MVC 控制器

编程入门 行业动态 更新时间:2024-10-21 16:38:28
本文介绍了AutoFixture 无法创建匿名 MVC 控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

代码:

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.

推荐答案

看起来,当使用 MVC 4 时,您必须以不同的方式自定义 Fixture 实例.

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 无法创建匿名 MVC 控制器

本文发布于:2023-11-10 02:24:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574086.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   AutoFixture   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!