How to Properly Test Controllers in ASP.net MVC that has database calls

前端 未结 3 1034
孤城傲影
孤城傲影 2020-12-31 20:52

I am working on an ASP.net MVC 3.0 Application. I am using MSTest along with Moq for unit testing. I have written all the test methods for my contr

3条回答
  •  旧时难觅i
    2020-12-31 21:08

    A controller never should call the database directly (one - not the most important - reason for that is that this makes the controller almost impossible to test...). Instead, I strongly advise you to refactor your code to enable testability in the first place and also to have proper Separation of Concerns: Put all your data access code into Repositories which you then access in your controllers via interfaces. This way, you can easiliy mock them with Moq.

提交回复
热议问题