Asp.net mvc 4 Autofixture-fixture.CreateAnonymous<;MyController>;()测试失败,请求异常非法

Asp.net mvc 4 Autofixture-fixture.CreateAnonymous<;MyController>;()测试失败,请求异常非法,asp.net-mvc-4,testing,asp.net-mvc-5,autofixture,Asp.net Mvc 4,Testing,Asp.net Mvc 5,Autofixture,我正在尝试使用Autofixture使用以下代码测试我的Mvc控制器:- var fixture = new Fixture().Customize(new AutoMoqCustomization()); fixture.Customize<ViewDataDictionary>(vdd => vdd.Without(x => x.ModelMetadata)); var sut = fixture.CreateAnonymous<MyController&

我正在尝试使用Autofixture使用以下代码测试我的Mvc控制器:-

 var fixture = new Fixture().Customize(new AutoMoqCustomization());
 fixture.Customize<ViewDataDictionary>(vdd => vdd.Without(x => x.ModelMetadata));
 var sut = fixture.CreateAnonymous<MyController>();
在谷歌上搜索了一段时间后,这似乎是由于使用了MVc4及以上版本,修复方法是将自定义行更改为

fixture.Customize<ControllerContext>(vdd => vdd.Without(x => x.DisplayMode));
fixture.Customize(vdd=>vdd.Without(x=>x.DisplayMode));
不管怎样,现在抛出一个新的异常

Exception has been thrown by the target of an invocation.
The method or operation is not implemented.
A request for an IntPtr was detected. 
This is an unsafe resource that will crash the process if used, so the
request is denied. A common source of IntPtr requests are requests for
delegates such as Func<T> or Action<T>. If this is the case, the expected
workaround is to Customize (Register or Inject) the offending type by
specifying a proper creational strategy.
检测到对IntPtr的请求。 这是一个不安全的资源,如果使用它,将导致进程崩溃,因此 请求被拒绝。IntPtr请求的一个常见来源是 委托,例如Func或Action。如果是这种情况,预期的结果是 解决方法是通过 指定适当的创作策略。 我现在似乎被卡住了

还有其他人来解决这个问题吗?或者知道一种不同的方法来轻松修复我的控制器


谢谢

这是由于我的.package文件告诉我我使用的是Autofixture 3.39.0,但测试项目实际上引用的是2.0.0


卸载旧版本并从解决方案中删除,然后重新添加解决了问题。

您需要自定义
ViewDataDictionary
ControllerContext
。所以基本上你需要保持两条线。查看这些答案以供参考:就我所知,@Enricoampidoglio,定制应该足够了。我刚刚尝试过,但我无法用AutoFixture 3.39.0和Microsoft.AspNet.Mvc 5.2.3重现这种行为。我们如何重现这个问题?谢谢大家的评论-马克,谢谢。深入研究,结果发现我有一个nuGet错误,所以即使nuGet报告了3.39-正在使用的Autofixture的实际版本是2.0-撕下并重新安装到3.39包-一切都很好。谢谢当然现在有了正确的版本CreateAnonymous需要更改为创建