将 T4MVC 与 TestHelper 结合使用时,如何测试渲染视图?

发布于 2024-09-10 13:05:21 字数 1265 浏览 9 评论 0原文

如果我得到的是 T4MVC_ActionResult,如何测试从控制器操作渲染哪个视图?在正常情况下,我应该能够直接使用 TestHelper 的方法,例如 示例

pooController.Details().AssertViewRendered().ForView("Details")

...但是,由于通过 T4MVC 我得到了 T4MVC_ActionResult 而不是 ViewResult,因此 AssertViewRendered<>().ForView("Details") 部分失败。如果我想测试调用了哪个视图,我有什么选择?


更新:

测试代码如下:

[TestMethod]
public void Theme_Controller_Details_Action_Returns_Details_View()
{
    var builder = new TestControllerBuilder();
    var mockThemeRepository = new Mock<IThemeRepository>();
    var themeController = builder.CreateController<Evalgrid.Website.Controllers.ThemeController>(mockThemeRepository.Object);
    builder.InitializeController(themeController);

    var result = themeController.Details();
    result.AssertViewRendered().ForView("Details");
}

我使用调试器在result行后设置断点,其变量类型为T4MVC_ActionResult,而themeController为Evalgrid.Website。控制器.ThemeController。请注意,我使用了控制器的完全限定名称。

我明白了:

预期结果为类型 查看结果。它实际上是类型 T4MVC_ActionResult。

我不知道发生了什么事。

How do I test which view was rendered from a controller action if what I get is a T4MVC_ActionResult? Under normal circumstances I should be able to directly use TestHelper's methods, like in the examples:

pooController.Details().AssertViewRendered().ForView("Details")

...but, since through T4MVC I get a T4MVC_ActionResult instead of a ViewResult, the part AssertViewRendered<>().ForView("Details") fails. What alternative do I have if I want to test which view was invoked?


UPDATE:

Here's the test code:

[TestMethod]
public void Theme_Controller_Details_Action_Returns_Details_View()
{
    var builder = new TestControllerBuilder();
    var mockThemeRepository = new Mock<IThemeRepository>();
    var themeController = builder.CreateController<Evalgrid.Website.Controllers.ThemeController>(mockThemeRepository.Object);
    builder.InitializeController(themeController);

    var result = themeController.Details();
    result.AssertViewRendered().ForView("Details");
}

I used the debugger setting a breakpoint after the result line, and its variable type is T4MVC_ActionResult, while themeController is Evalgrid.Website.controllers.ThemeController. Note that I have used the fully qualified name of the controller.

I get this:

Expected result to be of type
ViewResult. It is actually of type
T4MVC_ActionResult.

I don't know what's going on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-09-17 13:05:21

实际上,T4MVC 在这里应该没有什么区别。如果您直接实例化控制器并调用操作方法,无论您是否使用 T4MVC,您都会得到相同的结果。即您不会得到 T4MVC_ActionResult。

只有当您编写 MVC.Foo.Details() 时,您才会获得 T4MVC_ActionResult。这是因为 MVC.Foo 返回一个执行特殊操作的派生类的实例,而不是直接返回您的控制器类。

这有道理吗?

更新:我很困惑,在查看 TestControllerBuilder.CreateController 的源代码时,它有:

public T CreateController<T>(params object[] constructorArgs) where T : Controller
{
  var controller = (Controller)Activator.CreateInstance(typeof(T), constructorArgs);
  InitializeController(controller);
  return controller as T;
}

所以它直接实例化您传入的类型,它应该只调用您的正常操作。

关于您的代码的一个问题:您的“详细信息”操作方法是否采用任何参数?如果是这样,那就可以解释问题了,因为您在没有参数的情况下调用它,这将是在分部类中添加的 T4MVC 方法。

Actually, T4MVC should not make a difference here. If you directly instantiate your controller and call an action method, you'll get the same thing back whether you use T4MVC or not. i.e. you won't get a T4MVC_ActionResult.

It's only when you write MVC.Foo.Details() that you'll get a T4MVC_ActionResult. That's because MVC.Foo returns an instance of a derived class which does special thing, and not directly your controller class.

Does that make sense?

Update: I'm confused, as looking at the sources for TestControllerBuilder.CreateController, it has:

public T CreateController<T>(params object[] constructorArgs) where T : Controller
{
  var controller = (Controller)Activator.CreateInstance(typeof(T), constructorArgs);
  InitializeController(controller);
  return controller as T;
}

So it's directly instantiating the type that you pass in, which should just call your normal action.

One question about your code: does your Details action method take any parameters? If so, that would explain the problem, as you're calling it with no params, which would be a T4MVC method added in the partial class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文