Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests in a database driven CodeIgniter web-application

CodeIgniter comes with a Unit Testing class built in, and I would very much like to use it. However, almost all functions I would want to test interact with the database by adding records, deleting records, etc. How would I, for example, write tests for the 'create user' function without actually creating users every time I run the test?

Upon some further research, it seems I need to be using Mock objects for external services like the database, etc. I haven't been able to find much in the way of docs on how to do that besides this one forum thread: http://codeigniter.com/forums/viewthread/106737

Is there any actual documentation?

like image 214
Mala Avatar asked Feb 09 '12 17:02

Mala


1 Answers

If your database driver allows transactions, use them. Do whatever needs to be tested, then rollback (on success or failure).

I've found that it's hard to run unit tests with controller actions. If you find a good way of doing that, let us know!

like image 136
landons Avatar answered Nov 15 '22 03:11

landons