我一直在为一个简单的 Datamapper 类编写测试,并且我知道该方法正在工作,但是测试失败并给我错误“ Fatal error: Call to a member function fetchAll() on a non-object in C:\xampp\htdocs\Call log\tests\model_tests.php on line 13.
”显然,这不可能,因为我可以验证该方法是否有效。
这是它应该出错的代码:
function all() {
$calls = $this->pdo->query('SELECT * from calls');
return $calls->fetchAll();
}
这是我的测试代码:
class TestOfCallMapper extends UnitTestCase {
function testOfReturnsAll() {
$this->createSchema();
$mapper = Callmapper::getInstance();
$results = $mapper->all();
print_r($results);
}
private function createSchema() {
$mapper = CallMapper::getInstance();
$mapper->pdo->exec(file_get_contents('../database/create_schema.sql'));
}
private function destroySchema() {
$mapper = CallMapper::getInstance();
$mapper->pdo->exec(file_get_contents('../database/destroy_schema.sql'));
}
}
$test = new TestOfCallMapper('Test of CallMapper Methods');
$test->run(new HTMLReporter());
如果我这样做,它工作得很好:
$mapper = CallMapper::getInstance();
$test = $mapper->all();
print_r($test->fetchAll());