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 に答える