Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test MVC classes?

I'm new to unit testing in general, but wanted to implement it in MVC pattern (using PHP). Now I'm not sure how to approach this.

Should unit testing be built into the framework, or we just create a new folder called tests and include all necessary classes and unit test each one ?

In short, if there is a model M, it also has some coupling with the framework itself. So to test the model, should I include some portions of the framework in the unit tests ?

Are there some good code examples on how to go about accomplishing this.

like image 798
Nemo Avatar asked Jun 18 '11 15:06

Nemo


People also ask

How does MVC enable ease of unit testing?

ASP.NET MVC exposes extensibility APIs that enable developers to participate in the resolution and instantiation of controllers, and which enables Dependency Injection / IoC frameworks to be cleanly integrated within this process.


3 Answers

Should unit testing be built into the framework, or we just create a new folder called tests and include all necessary classes and unit test each one ?

You should definitely create a separate folder for it. Cluttering up production code with tests is generally not a good idea for performance and debugging reasons.

So to test the model, should I include some portions of the framework in the unit tests ?

The least the better. Unit tests should require little to no dependencies. If class A depends on B, you should mock B to ensure that if B fails, it doesn't make A fail too.

The main advantage to unit tests (when done correctly) is that it allows you to easily pinpoint the problem. If A fails because of its B dependency, you will first look at A, then B. Again, if B depends on C and C fails, you will have to look into A, B and then C. This pretty much ruins one of the greatest advantage to unit testing. If all tests are done properly, a failure in C will not cause a failure anywhere else than in C, so you will have a single class to lookup to fix the problem.

To really make your code error-proof, you can use unit tests in conjunction with PHP assertions:

$return = $this->addOne($some_param);
assert('$return == $some_param + 1');

By the way, there is no difference between unit testing MVC as opposed to unit testing in general.

like image 63
netcoder Avatar answered Oct 07 '22 22:10

netcoder


Should unit testing be built into the framework, or we just create a new folder called tests

If you're using a third party framework, it will usually include some unit testing helpers, but you'll want to put your own test classes into a separate folder so that (for example) you can roll a release of your software for distribution that does not include them.

include all necessary classes and unit test each one ?

You'll generally have one test class per application class. So, if you have a model class M, you'll have a test class M_Test (or whatever naming convention you adopt.)

If you're not already familiar with PHPUnit, you'll want to grab it and read their docs.

like image 30
Alex Howansky Avatar answered Oct 07 '22 22:10

Alex Howansky


Unit testing should be part of the MVC framework. For example have a look at the Unit Testing Class chapter of the CodeIgniter user guide.

like image 43
Justin Ethier Avatar answered Oct 07 '22 22:10

Justin Ethier