0

我一直在为一个简单的 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());
4

2 回答 2

1

您的 PDO 查询返回 false 因此它不是 PDO 的实例,而是一个布尔值,试试这样的事情!

function all() {
    $calls = $this->pdo->query('SELECT * from calls');
    if($calls === false)
    {
        throw new Exception("Unable to perform query");
    }
    return $calls->fetchAll();
}

然后在你的范围内TestOfCallMapper你可以做:

function testOfReturnsAll()
{
        $this->createSchema();

        $mapper = Callmapper::getInstance();
        try
        {
            $results = $mapper->all();
        }catch(Exception $e)
        {
            //Some Logging for $e->getMessage();
            return;
        }
        //Use $results here        
}
于 2010-11-18T19:02:14.620 回答
0

pdo 查询显然失败了,因此您试图在 false 上调用 fetchAll。

我会检查它为什么失败。

于 2010-11-18T18:14:09.247 回答