【问题标题】:Need help with error using MvcContrib.TestHelper's ShouldMapTo() generic extension method使用 MvcContrib.TestHelper 的 ShouldMapTo() 通用扩展方法需要帮助解决错误
【发布时间】:2010-03-05 20:48:35
【问题描述】:

我正在尝试使用 MvcContrib.TestHelper ShouldMapTo<TController>() 扩展方法对我的路由进行单元测试,但我的测试失败并显示以下错误消息:

failed: Method MvcContrib.TestHelper.RouteTestingExtensions.ShouldMapTo:
type argument 'ReviewController' violates the constraint of type parameter
'TController'.

但 ReviewController 确实满足约束条件。它继承自一个名为 SmartController 的类,该类继承自 System.Web.Mvc.Controller。因此,我不知道如何解决此错误。

这是我的单元测试:

[Test]
public void Should_map_review_controller_routes_correctly()
{
    MvcApplication.RegisterRoutes(RouteTable.Routes);
    "~/reviews"
        .ShouldMapTo<ReviewController>(c => c.Index());
}

这里是 ReviewController 类的声明:

public class ReviewController : SmartController<Review, ReviewForm>
{
...
}

以及SmartController类的声明:

public abstract class SmartController<TModel, TForm> : Controller
    where TModel : new()
{
...
}

只是为了笑,我尝试从继承层次结构中删除 SmartController,以便 ReviewController 直接从 Controller 继承,但仍然抛出错误。

有谁知道我做错了什么?

【问题讨论】:

    标签: asp.net-mvc unit-testing asp.net-mvc-routing mvccontrib-testhelper


    【解决方案1】:

    我将此方法与 MVC 2 和使用 MVC 2 构建的 MvcContrib 一起使用,一切正常。我发现了这个问题:

    http://groups.google.com/group/mvccontrib-discuss/browse_thread/thread/356203db654fa4bd?pli=1

    您是否在 MVC 2 中使用旧的 MvcContrib 程序集(使用 MVC1 构建)?如果是,您应该下载 MvcContrib 二进制文件或 MVC 2 的源代码。

    【讨论】:

    • 感谢 LukLed。那成功了。我必须更新我的 Rhino.Mocks 程序集以使用新的 MvcContrib 版本,但有了它,我的测试现在通过了,所以我很高兴。我真的很感激。
    • 如果您将 MVC 2 MvcContrib 程序集与 MVC 3 解决方案一起使用,您将遇到相同的情况。更新到最新的 MvcContrib 版本,一切都会好起来的。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2021-09-10
    相关资源
    最近更新 更多