Reputation: 1380
I have a long experience with programming and decided that in my next project I will use unit testing. I am creating an application using php with CodeIgniter and oracle as a rdbms. All my models have basically only methods that work with the database: CRUD methods, nothing fancy. Ok so the question is how should I test these? Are they worth testing ? Because when the query is wrong I will get a php error, and basically all my tests will pass because my queries are correct. What should I focus and what should I expect when testing models?
e.g.: simple method:
public function register($username, $password, $email)
{
$hash = $this->_generate_hash();
return $this->add(array(
'username' => $username,
'password' => hash('sha256', $this->config->item('salt') . $password . $hash),
'email' => $email,
'hash' => $hash
));
If the query is ok this insert will always work and if i add test params to the function it will always pass, but the test passes because the SQL is ok, or how should I test this? }`
Upvotes: 2
Views: 1522
Reputation: 24579
When testing models, I try to think about two things: code coverage and alternate parameters.
Code Coverage: It is important to try to achieve a high level of code coverage. This allows you to figure out EXACTLY what can happen in different execution situations and how you handle them. Often times, I find areas that are hard to test which after a little review come to the realization that the way it is written is not optimal. A quick refactor of my code usually ends up making everything look cleaner, work better, and also be more testable.
Alternate Parameters: You may feel you have complete control over data being sent to your CRUD functions, but how will they handle the situation in which it's not what you expect? You said when the query is wrong I will get a php error
but is that what you really want to happen? Wouldn't it be better to catch those errors using exception handling and deal with them on your own terms rather than sudden and immediate death of your script execution? Or worse, something is vulnerable to SQL injection and you didn't know because you never created a test case?
Those are a couple things I personally like to think about when unit testing my models.
Upvotes: 2