Unit Testing & Mock Objects |
[eluser]Aniket[/eluser]
I even read that article too...its applicable with an Object Oriented Application. where in php functions are used for abstraction of logic or database handling. But with CI its a problem as the way CI works. we just get instance of database using a single line $this->load->database(). May be i need to look into the /system/Database directory and check if i can figure out a way around.
[eluser]Aniket[/eluser]
:-) Finally after lot of hacking CI source code i was able to figure out how to create a Mock object :-). Will post my findings and code in couple of days. But right now i am too excited and trying to inject this mock object into the model and perform unit testing on it. :-)
[eluser]Aniket[/eluser]
Well this what i tried and was successful. Code: <?php But i had couple of problems: 1) I had to make changes to the database configuration to point to the desired database. 2) I could not instantiate the db's instance from the CI's super object so i decide to include the DB file and get the instance. Haven't got much time to think over it. If anybody finds a solution please post it. Thanks in advance
[eluser]Unknown[/eluser]
Thanks Aniket! This will work well for some tests but I wish they had an easier way to hook in to the database. I feel like the only way to truly be able to run unit tests you would have to create your own CodeIgniter driver that emulates the necessary functionality. The issue I am running into is I have a model: Code: class User extends Model { You should be able to test that model from a unit test by calling it directly from within the test: Code: $user = $ci->user->getUser(1); So your Data class would have to have the get_where function and the other functions within CI in order to handle that (I think). Or am I wrong by design? In the MVC framework for ASP.NET there is an additional layer so you have controller->service->model->database instead of controller->model->database here (although CI's drivers may work as a service layer) I guess an alternative would be to have the User model just interface against another class (IUser) and make two versions that implement it - one that touches the database and one that doesn't, but I feel like that would be pointless unless you did Integration Testing as well.. AHHH! I'm going back to BASIC.
[eluser]ch5i[/eluser]
Hello, after a lot of trial & error I got this finally to work. Thanks for your post Aniket, that got me started. So, here it is: A simple example on how to mock the CI database class. User model (test subject) Code: <?php Unit Test Case Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Sidenote: I use SimpleTest, set up as described in the Wiki. Well, almost as described, I do not autoload the simpletester library (step 5), but have a unit test controller which loads it and calls the Run() method, so that the tests are not run everytime i call a controller.
[eluser]Unknown[/eluser]
@Aniket @ch5i thanks guys, you really helped me with mock object by the way, I got an error saying "Call to a member function result() on a non-object" in my model which is: Code: return $query->result(); anyone know why? thanks anyway, here's my test case: Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |