Testing controllers - can you show me code?
Posted: Fri Nov 30, 2007 1:59 pm
I've read and seen many examples which demonstrate how to test single classes with few dependencies, such as a model.
1) Initialize the environment inside setup/teardown such as creating and destroying databases.
2) Run each test - voila!
What about when you want to test something more complicated like a application controller?
For example mine are dependent on the model and view, not to mention the front controller and it's request/response objects and additionally the registry and it's various objects and possibly several other layers.
My models are static functions stuffed inside a class but are invoked statically in the controller. Obviously it would make sense to mock/stub or whatever these functions as I care less about the database than I do about the application logic at this point. So having them return phantom values as opposed to connecting to DB, querying, etc.
So I create a stub object with all the methods in place and simply have them return valid values.
I've read SimpleTest and it's Mock object section. I like the idea but I don't think that would work in my controllers because the controllers hardcode some objects such as the model and view, so would it make sense to create a stub class of each model/view and include that instead of the actual mode/view classes?
Incase your wondering why I said SimpleTest Mock's won't work (at least to the best of my knowledge) is because my controller looks something like:
Testing this controller would look something like (I assume):
Given the above snippet of psuedo-code I don't see how I would effectively Mock an object using SImpleTest and it's mocking functionality because it acts as a class factory and actually generates the class interface BUT with a prefix of Mock. Obviously when a Model is invoked inside the myAction it cannot use the Mock created in the setup/teardown of the unit test so a manually created stub class is required instead - unless I have missed something in SimpleTest docs???
I have been trying to determine the difference between Stubs and Mocks since Maugrim posted the question in the forums. If the above problem cannot be solved technically using SimpleTest Mocks - than I would need to create the stub manually, correct??
In which case are stubs and mocks not two different techniques, each required under different situations???
1) Initialize the environment inside setup/teardown such as creating and destroying databases.
2) Run each test - voila!
What about when you want to test something more complicated like a application controller?
For example mine are dependent on the model and view, not to mention the front controller and it's request/response objects and additionally the registry and it's various objects and possibly several other layers.
My models are static functions stuffed inside a class but are invoked statically in the controller. Obviously it would make sense to mock/stub or whatever these functions as I care less about the database than I do about the application logic at this point. So having them return phantom values as opposed to connecting to DB, querying, etc.
So I create a stub object with all the methods in place and simply have them return valid values.
I've read SimpleTest and it's Mock object section. I like the idea but I don't think that would work in my controllers because the controllers hardcode some objects such as the model and view, so would it make sense to create a stub class of each model/view and include that instead of the actual mode/view classes?
Incase your wondering why I said SimpleTest Mock's won't work (at least to the best of my knowledge) is because my controller looks something like:
Code: Select all
function myAction()
{
$smarty-assign('listings' MyModel::queryListings($keywords));
}Code: Select all
//
// Setup environment in test case setup/teardown
$request = new Request();
$request->setValue('name', 'Jow Schmoe');
$front = new Front();
$front->setRequest($request);
//
// Execute action on controller
$obj = new myController();
$ret = $obj->myAction();
//
// Run tests
ASSERT($ret == false); // Make sure controller doesn't return FALSE
ASSERT($response->getHeader('Location') == ''); // Make sure controller does a redirectI have been trying to determine the difference between Stubs and Mocks since Maugrim posted the question in the forums. If the above problem cannot be solved technically using SimpleTest Mocks - than I would need to create the stub manually, correct??
In which case are stubs and mocks not two different techniques, each required under different situations???