Making sure a view exists

前端 未结 5 1112
执念已碎
执念已碎 2021-02-06 09:37

I\'m currently looking into unit testing for a new application I have to create. I\'ve got the basic testing going nicely (testing the ActionResult classes is pretty nice). One

5条回答
  •  名媛妹妹
    2021-02-06 10:12

    You could try to use the FindView method of the ViewEngineCollection object you have in the ViewResult to check if the MVC framework can locate the View.

    As other have suggested I think this 3'rd Assert (that the view actually exists) is not something that will add real value to your tests, but nevertheless, here's the code to check for existance:

    var viewEngineResult = result.ViewEngineCollection.FindView(controller, result.ViewName, result.MasterName);
    if (viewEngineResult == null)
        ... not found ...
    

    Hope this helps.

提交回复
热议问题