When I applied each of these techniques to my own application...verious problems popped up...namely testing a model which is dependent on a database access object.
Code: Select all
class MyModel{
function selectAllListings()
{
$db->query('SELECT * FROM things');
$arr = $db->execute();
$results = array();
foreach($arr as $item){
// TODO: Do stuff to resultset before returning
}
$results;
}
}When I mocked the database object, obviously calls to it return nothing...and when notihng is returned from the execute() above, guess what happens to the foreach? Chokes because it expects an array. Additionally, I need the for look to fire to properly test the behavior of the method.
The only solution I can see, is mocking everything *but* the database object.
The horrow stories I've read about tests taking 15 minutes...well thats either a requirement or I have to find a more efficient approach. So I thought. Why not group your tests into categories?
When I'm working on a project, the interface usually dictates the categorization into modules like:
1) Dashboard
2) Reports
3) Contacts
4) Templates
5) Admin
Each of these modules usually are composed of various classes, like models, views, controllers, etc...
If I grouped my tests like the above and only ran tests which are needed...I would save a ton of processing time and likely avoid the 15 minute horor stories all the while repeaing the benefit of a properly tested model.
How do you deal with it? How do you test your models? I've read a lot of people in forums,articles, etc suggest mocking the database abstraction layer, data access objects, etc, but I have yet to see any concrete examples...do you know of any or care to share your experiences?
Cheers