【发布时间】:2016-02-09 05:50:29
【问题描述】:
我有一个带有以下代码行的 MVC6 控制器:
string link = Url.Action("Profile", "Account");
当我对该控制器进行单元测试时,该行失败并出现错误:
Value cannot be null.
Parameter name: helper
我真的不想模拟 Url.Action 响应,而且我认为我不能,因为它是一种扩展方法(静态方法)。我希望它像在 Web 环境中一样运行和返回。
在我的单元测试中实例化控制器时,我需要做什么才能使上面的行按预期执行?
我发现我可以在我的单元测试中做这样的事情:
controller.Url = new UrlHelper(new ActionContextAccessor(), new DefaultActionSelector(...));
但我不知道设置 ActionContextAccessor 和/或 DefaultActionSelector 需要什么(这需要更多类型,我不确定从哪里获取或如何实例化)。
有人已经这样做了吗?
谢谢, 凯文
【问题讨论】:
标签: c# asp.net-mvc unit-testing