So I have a very simple (at least right now it is) model that returns a contact via it’s primary key ID:
class Model_Contact extends \Fuel\Core\Model
{
public function get_by_id($contact_id)
{
return Entity_Contact::find_by_pk($contact_id);
}
}
The Entity_Contact class looks like this (irrelevant array content omitted):
class Entity_Contact extends \Core\Entity_Base
{
protected static $_table_name = 'contacts';
protected static $_properties = array(...);
protected static $_public_settable_properties = array(...);
protected static $_rules = array(...);
}
Note: \Core\Entity_Base extends \Fuel\Core\Model_Crud
I might use this in a controller like so:
$model = new Model_Contact();
$contact = $model->get_by_id(4);
I know that in order to unit test this, I should mock out the actual database call (Entity_Contact::find_by_pk), but I’m not sure how to do this. Since I’m using Fuel’s Model_crud functionality (where the DB accessors are actually a part of the domain object model), I’m not sure that I can completely mock the database—or maybe I’m missing something.
So the question: how would you write a test for Model_Contact::get_by_id()?
Thanks in advance!
Your test may have to create the objects so you can test your methods.
It’s not the most glorious thing in the world, but it would accomplish testing your method… meh?