I am learning MVC design pattern as an android developer and I've been seeing that it facilitates the testability of code. I understood this concept, but in real case I'm frustrated.

Can someone please explain how this is achieved in a real example?

有帮助吗?

解决方案

MVC stands for model view and controller. One can view MVC as an extension of Single Responsibility principle. The reason for a change in each of these components would be different.

The Model: models the business. The details of http and rest are not supposed to be in this component. Ideally if you decide to make your application as desktop application then you should be able to reuse the model component.

The View: has the details of the view. They are mostly about input/output. This component can depend on the model component. After all you are showing the data from the model here.

The Controller: Binds the model and views together by taking of the HTTP concerns such as return code.

I am learning MVC design pattern as an android developer and I've been seeing that it facilitates the testability of code.

Now coming to your question of how this improves testability. There are different types of testing. Understanding the capabilities and limitations of different types of testing methods are important in answering this question. There are many types of testing, and we could look at unit testing, integration testing and system testing.

Unit Testing: Ideally unit tests have to run quickly and give the results quickly. The details of HTTP call or database call would be often mocked out. This would be very suitable to test business logic. These are often fully automated and integrated with the build.

Integration Testing: Integration tests integrate different aspects of the business and tests. They could integrate infrastructure aspects as well for testing. They are bit more time consuming to execute that unit test. They can often be fully automated.

System Testing: This is testing the system fully. They are often time consuming. They could be automated using tools like selenium. They are many a times manually don.e

How testing is made easier: When we separate the code in to these 3 components (model, views and controllers). The model would now contain all the business logic. A huge percentage of your code base would be in the model component. By mocking out the infrastructure code (such as talking to DB or making other HTTP calls, etc...), this component can be fully unit tested.

The presence of UI would make unit testing quite difficult if not impossible. Also testing of the web server aspects of the application by unit testing is also a bit difficult. Now having separated that code in to view and controllers will make the model easy to unit test would be another way of looking.

This was we are able to test most of the code with highly quick unit testing, and test the other aspects with fewer integration and system tests.

Summary:

  • In MVC, model depends on nothing, view depends on model, and controller depends on view & model.
  • If A is dependent on B, and B is not unit testable, then A is not unit testable.
  • View and Controller are not unit testable (or at least difficult to unit test).
  • In MVC, most of the code will be in the model.
  • By Making model NOT dependent on view and controller, model (most of the code) becomes unit testable.
许可以下: CC-BY-SA归因
scroll top