How unit testing with Lithium
At work we’re starting to use Lithium for our main product. So once I found out, the first thing I wanted to do was to find out how I could unit test with it. In some cases it makes life really easy in other cases it made it bitch. So here’s what I’ve figured out so far. If you know a better way please tell me.
Original Controller
This is a simple controller that just sets a view variable called test with “test” as it’s content.
Mocks
To unit test the controller I’ve found that you need to mock the controller to allow you to use the dependency injection ability to use a mock response class to stop the view from being outputted. Below I’ve included a basic starting point for the mock controller in which it changes the response to the mock response and I added a getter function to allow me to access internal variables such as the render.
Here is the mock response class which just disables the rendering of the page view.
So now we have a mock controller that we can run tests on that won’t output HTML on every test. Now I’ll create a simple test which it checks to see if the test view variable has been set.
Mocking models for use in a controller test is more difficult, it looks rather nasty as lithium’s models are called statically. While lithium does allow you to use a test connection with your tests it means you can’t just test the logic of the code without testing the database layer. While I do agree that you should also do tests with your database layer, I also think you should do tests without a database connection so as you can see what is failing, the data being inputted into the model or the database layer. So after some trying to do some namespace magic, I finally remembered you can call static classes from a variable string. So to use a mock model I decided use the lithium dependency injection system classes array and retrieve the class name from it.
Controller
Mock Controller
In the mock controller we just replace the articles class with our mock Article class.
Mock Model
Here is a very simple mock Article class which just replaces the find method and returns two sets of data. In reality you would want a much better find and replace the insert as well. Currently I haven’t done too much in mocking models I just wanted to be able to do, due to how nasty it seems we’ve went for just using Fixtures.
Fixtures
Now I’ve shown how to do the unpopular mocking of the model class, I’ll link you to the Lithium fixtures, instead of basically duplicating what the author of the fixture plugin has to say you may as well get it from the horses mouth here.
Conclusion
Lithium unit testing is pretty simple, although this is a rather simple run though, I hope to post other snippets about unit testing when I run into stuff I need to figure out. I am sure there is probably stuff I could be doing better, if you know anything please do alert me.
-
buy-steroids-uk-dot-co reblogged this from icambridgeblog
-
dortheauo90 liked this
-
kathyshe901 liked this
-
icambridgeblog posted this