【发布时间】:2016-01-20 10:14:04
【问题描述】:
我正在尝试使用 Autofixture 通过以下代码测试我的 Mvc 控制器:-
var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.Customize<ViewDataDictionary>(vdd => vdd.Without(x => x.ModelMetadata));
var sut = fixture.CreateAnonymous<MyController>();
抛出以下异常
Exception has been thrown by the target of an invocation.
The method or operation is not implemented.
经过一番谷歌搜索后,这似乎归结为使用 MVc4 及更高版本,解决方法是将自定义行更改为
fixture.Customize<ControllerContext>(vdd => vdd.Without(x => x.DisplayMode));
这会引发新的异常
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.
我现在似乎被困住了。
有其他人遇到过这个问题并解决了吗?或者知道另一种方法可以轻松固定我的控制器?
谢谢
【问题讨论】:
-
您需要自定义
ViewDataDictionary和ControllerContext。所以基本上你需要保留两条线。请参阅这些答案以供参考:stackoverflow.com/a/14989866/26396 和 stackoverflow.com/a/6592112/26396 -
@EnricoCampidoglio 据我所知,定制应该就足够了。
-
我刚刚尝试过,但无法使用 AutoFixture 3.39.0 和 Microsoft.AspNet.Mvc 5.2.3 重现此行为。我们如何重现该问题?
-
感谢所有 cmets - Mark 谢谢。向下挖掘,结果发现我有一个 nuGet 错误,所以即使 nuGet 报告 3.39 - 正在使用的 Autofixture 的实际版本是 2.0 - 撕掉并重新安装包到 3.39 - 一切都很好。谢谢
-
当然现在使用正确的版本 CreateAnonymous
需要更改为 Create
标签: asp.net-mvc-4 testing asp.net-mvc-5 autofixture