2

Disclaimer: I'm new to unit testing and just getting my feet wet. That being said, I understand 100% is not always possible, but I'm curious if there is a solution to code coverage with if/else statements such as this or if they are just untestable.

The function:

public function getAll() 
{

    // Query table for all rows sorted by name
    $select = $this->select();
    $select->order('name ASC');
    $rowset = $this->fetchAll($select);

    // Validate and return row
    if($rowset->current())
    {   
        // Return rowset
        return $rowset;
    }
    else { return false; }

}

The Unit Test

public function testCanGetAll()
{
    // Try to get all states
    $result = $this->model->getAll();
    $this->assertNotNull($result);
    $this->assertNotEquals(false,$result);
}

The Result

enter image description here

As you can see, I can't cover the return false; line. The only way I can think of to test it is to rename my database table or something drastic like that.

Is there another way to write functions with if/else statements (which are a fairly common practice) which allows for easier code coverage?

[EDIT] Perhaps it's better to not check results in the function, and just return it? Whatever calls this function has to do it's own checking?

public function getAll() 
{

    // Query table for all rows sorted by name
    $select = $this->select();
    $select->order('name ASC');
    $rowset = $this->fetchAll($select);

    // Return rowset whether valid or not
    return $rowset; 

}

1 Answer 1

4

Create a db context where the result would return no rows, then in your second test method create a row in that db context.

Its usually not a good idea to test against your actual application's DB

you should be cloning it for your tests so that you can manipulate the data to test how the application will respond to varying data contexts.

--EDIT----

Following this guide may help: http://framework.zend.com/manual/ru/zend.test.phpunit.db.html

Sign up to request clarification or add additional context in comments.

10 Comments

That is easy for my functions like getByID() or getByName() where I can pass in a bad value and assert that way. But this takes no parameters and just returns all rows. In that instance, I have no context to modify. Also, my application is read-only and doesn't do any inserts/updates so I didn't see a need for cloning.
you may want to refactor your code a little to make it more testable. How do you save your DB config? If you use a Singleton or static interface you could swap out what db to use. In an ideal situation you'd dump the structure and create a new database, then seed that database with predefined test records, and easily test with that by adding or removing rows.
In this case, I'm using Zend Framework (PDO/MySQL). My models extend the Zend_Db_Table_Abstract object so they work similar to an ORM. Regardless, that's too specific and irrelevant. How can I refactor that to be testable?
How do you configure which database your application should use?
I know thats not the issue, i am saying you should not be using your production/development db for testing. You should have an empty db or a cloned db on which you can operate for testing. You should remove all the rows in the table your code tests, run your test, then add a row, and run your test. this will allow you to test both states of your example function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.