开发者

ViewName Returned is Empty

开发者 https://www.devze.com 2023-02-11 13:07 出处:网络
I am performing a unit test and checking if my controller action returns the correct view but for some reason it is always returning empty string.

I am performing a unit test and checking if my controller action returns the correct view but for some reason it is always returning empty string.

  [When(@"when I go to the search page")]
        public void WhenTheUserGoesToTheSearchScreen()
       开发者_JAVA技巧 {
            _controller = new HomeController();
            _result = _controller.Search(); 
        }

        [Then(@"the search view should be displayed")]
        public void ThenTheSearchViewShouldBeDisplayed()
        {
            Assert.AreEqual("Search",(_result as ViewResult).ViewName); // ViewName is empty!
        }


The ViewName property will always be empty unless you pass the string name of the view when its returned from your controller action, I.E:

public ActionResult Search()
{
    return View("Search");
}

If you just return View() then ViewName will be an empty string. It's annoying (especially when using MVContrib) but it is what it is.

0

精彩评论

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

关注公众号