Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing CI query object #1

Open
fastzen opened this issue Apr 24, 2013 · 0 comments
Open

Testing CI query object #1

fastzen opened this issue Apr 24, 2013 · 0 comments

Comments

@fastzen
Copy link

fastzen commented Apr 24, 2013

I'm working my way through the book (very nice work) in order to retrofit tests into some CI code that I've taken over.

Everything goes fine until I try mocking a CI query object, for which the book has no example.

My additional function for Value_model:

    public function getAnyForTracker($tracker_id)
{
    $sql = "select * from trackers where tracker_id = ?";
    $query = $this->db->query($sql, array($tracker_id));
    return $query->result();
}

and my test is:

public function testGetAnyForTracker()
{
    $dbdata = array( array( 'tracker_id' => 'website_visits', 'value' => '127.0.0.1', 'created_at' => '' ),
                     array( 'tracker_id' => 'website_visits', 'value' => '66.121.684.92', 'created_at' => '' ),
                     array( 'tracker_id' => 'website_visits', 'value' => '84.124.152.11', 'created_at' => '' ) );

    $model = new Value_model();
    $model->db = Mockery::mock($model->db);
    $result = Mockery::mock('db result');

    $result->shouldReceive('result_array')->once()->andReturn($dbdata);
    $model->db->shouldReceive('query')
              ->once()
              ->with('tracker_id')
              ->andReturn($result);

    $result = $model->getAnyForTracker('website_visits');

}

I then get the following result:

1) Value_modelTest::testGetAnyForTracker
Mockery\Exception: No matching handler found for CI_DB_mysql_driver::query("select * from trackers where tracker_id = ?", Array). Either the method was unexpected or its arguments matched no expected argument list for this method

I don't actually get round to making assertions about the return value as I'm not sure how to correct that error in the result.

Any pointers would be gratefully received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant