在 playframework 2.1 中,是否可以测试某个操作以确保渲染的 View 是我期望的 View ?
在 ASP.NET MVC 3 中,AssertViewRendered().ForView("view")
正是测试了这一点。我们可以在2.1游戏中做到这一点吗?怎么办?
我想要实现的非常基本的 MVC 3 示例:
// Given the action GetUsers that renders the view "Users", I would like to assert
// this view as the one I expect and no other.
public class UserController
{
public ActionResult Index() {
return View("Users");
}
}
[Test]
public void GetUsersRendersCorrectView()
{
// Setup
var userService = new Mock<UserService>();
var userController = new UserController(userService.Object);
// Excercise
var result = userController.GetUsers();
// Verify
result.AssertViewRendered().ForView("Users");
}
谢谢。
最佳答案
Play 中的 View 渲染只是一个方法调用(模板被编译为简单的 Scala 函数)。
没有什么可以阻止您使用“手动构建”功能来实现 View 渲染。
因此,Action 返回的 Result
不知道内容是来自模板还是其他内容。这就是为什么 Play 无法实现您想要实现的那种断言。
关于java - 在 Playframework 2.1 中测试预期渲染 View 的 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689211/