Making sure a view exists

前端 未结 5 1104
执念已碎
执念已碎 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.

    0 讨论(0)
  • 2021-02-06 10:28
    • No, I don't think you should split the test up as long as its just mainly a third assert and not very much more code.

    • Yes, I think a more descriptive name would be helpful.

    • Since you've verified it has the correct view name already, wouldn't simply successfully rendering the view verify its existence?

    I think that its great you are working on complete test coverage but here I feel like there might be more effective use of your time if you were able to move on to the part where you verify that the units that perform the actual specific login functions (such as verifying password hashes or whatever) are working correctly.

    0 讨论(0)
  • 2021-02-06 10:29

    I totally agree with Jason, but I don't think what you are trying to do actually contributes to test coverage. After all, rendering it and testing behavior will already cover whether or not it exists.

    Lots of developers go overboard when they first get bitten by the test-driven development bug. They want test failures to tell them exactly what is wrong so they don't have to dig in and debug. That isn't the primary purpose of testing; testing is for verifying correct behavior so you don't ship bad software. When something is wrong, then you can debug. There's no need to have a test harness so specific that the testing engine knows exactly what the issue is.

    0 讨论(0)
  • 2021-02-06 10:30

    Knowing that the view exists in the solution is not terribly useful. What you really care about is that the view will be deployed, since your users (I hope) don't run your site within Visual Studio. In other words, what you are asking for is not a unit test, but an integration test. Therefore, you should use an appropriate tool for the job. Consider a web testing framework like Selenium.

    0 讨论(0)
  • 2021-02-06 10:35

    If you're using beta, your code-behind file will create a class for the View which you can check for using reflection.

    Otherwise, you could check for the file to exist in the right location.

    0 讨论(0)
提交回复
热议问题