开发者

Why is my controller test not working, the view name is coming back empty

开发者 https://www.devze.com 2023-04-12 22:15 出处:网络
Doing a simple test to verify the view name for a controllers action: var c开发者_运维百科ontroller = new UserController();

Doing a simple test to verify the view name for a controllers action:

  var c开发者_运维百科ontroller = new UserController();

  var result = controller.Login() as ViewResult;

  Assert.AreEqual("Login", result.ViewName);

The result.ViewName is coming back with "", why would this be?


Are you specifying the view name in the controller Login method or are you leaving it at the default value (which is "")?

If you leaving it at the default value, which is common, you need to test for String.Empty instead of "Login".


if you have a view like this

 public ActionResult Index()
  {
   return View();
  }

then the ViewName property will take it's default value that is "" and if you specify the view name like this it'll work fine

 public ActionResult Index()
  {
   return View("Index");
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号