代码:
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 控制器
发布评论