問題描述
MVC 為什麼要單元測試控制器 (MVC Why unit test controllers)
just a Provocative Question
why do you thing we should unit test controllers in MVC why not just write test against models or service layer.
‑‑‑‑‑
參考解法
方法 1:
A good example should be this one (in BDD‑style):
Given the user 'snehal' does not exists When I create a new user with credentials 'snehal' and 'so@123' And I log in with this user Then I should see a welcome page
This scenario is expecting that new user should see a welcome page when they log in for the first time. At least for me, this is the Controller's job and is a nice feature that I would like to make sure it is works.
方法 2:
One point is testing your routes.
方法 3:
The controllers can contain quite a bit of critical functionality. If your app doesn't have critical logic here, it may not be necessary to unit test quite so heavily. But I'd at least write a few tests to make sure your routes are set up correctly.
(by Snehal、goenning、Mariusz、Wes P)