Pergunta

Eu tenho escrito um teste para uma classe simples de Datamapper e sei que o método está funcionando, no entanto, o teste falha e me dá o erro "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."Obviamente, isso não pode estar certo, porque posso verificar se o método funciona.

Aqui está o código em que ele está errando:

function all() {
    $calls = $this->pdo->query('SELECT * from calls');
    return $calls->fetchAll();
}

Aqui está o meu código de teste:

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());

Se eu fizer isso, funciona bem:

    $mapper = CallMapper::getInstance();
    $test = $mapper->all();
    print_r($test->fetchAll());
Foi útil?

Solução

A consulta de PDO está evidentemente falhando, portanto você está tentando chamar Fetchall em False.

Eu verificaria por que está falhando.

Outras dicas

Sua consulta de PDO está retornando falsa, portanto, não é e a instância do PDO, mas um booleano, tente algo assim!

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

E então dentro do seu TestOfCallMapper você pode fazer:

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

        $mapper = Callmapper::getInstance();
        try
        {
            $results = $mapper->all();
        }catch(Exception $e)
        {
            //Some Logging for $e->getMessage();
            return;
        }
        //Use $results here        
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top