Testing without a real database?
Posted: Mon May 01, 2006 12:42 pm
I have a simple class which which encapsulates access to the MySQL database.
The constructor creates the connection and selects the db using the built in PHP functions. I also have the following two functions:
My question is, how do I test these two functions without using a real database? i.e. assert that getRow() will return false if $this->query is empty. (The constructor will exit if it cannot make the connection but I want the test to be self-contained and work without a real db).
Ultimately, I want to mock the results returned from the database to test that the data is handled properly.
Are partial mocks the answer? But how do I partially mock the class I am testing?
I'm using SimpleTest BTW.
Thanks in advance!
The constructor creates the connection and selects the db using the built in PHP functions. I also have the following two functions:
Code: Select all
public function fetch($sql)
{
$this->query = mysql_query($sql,$this->db);
}
public function getRow()
{
if ($row = mysql_fetch_array($this->query, MYSQL_ASSOC))
return $row;
else
return false;
}Ultimately, I want to mock the results returned from the database to test that the data is handled properly.
Are partial mocks the answer? But how do I partially mock the class I am testing?
I'm using SimpleTest BTW.
Thanks in advance!