开发者

Unit testing implies testing controllers/views too?

开发者 https://www.devze.com 2023-01-11 21:13 出处:网络
I want to understand exactly what unit test means. From what I have understood so far it implies testing a model and all its public methods.

I want to understand exactly what unit test means.

From what I have understood so far it implies testing a model and all its public methods.

Is that correct?

An开发者_JAVA百科d what about controllers? Does unit test means testing the controllers/views too?

Could someone enlighten me on this.


Basically, if it has business logic that is non-trivial then it should be unit tested.

As you test the controller, if you have already tested lower levels, then you can mock out the lower levels so that your test will go quicker, but, everything should have a test, all the public and protected methods, not just the public ones, IMO.


Unit testing as a concept exists outside of MVC.

So yes, you want to unit test all of your code, not just the model.


Unit testing as the name suggest is testing a code unit.

Theoretically you can test all the code, but sometimes this is hard to do.

If a controller should output something to the view you can test that.

Sometimes you may want to create a mock object to make things easier. For example, you can mock a DAO or a HttpResponse.


Usually your model is 'dumb' in that it doesn't contain any logic, just properties and state. So the unit tests are centered around testing your repository and services which interact and use the domain objects and (if you use them) summary model objects.

You can also test the controllers which call these services for complete coverage. It all depends on how much of the project you would like to see tested. Testing the views is slightly harder, for .NET web-based projects you can use frameworks like Selenium and Watin which integrate into unit testing frameworks.

0

精彩评论

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