dimanche 28 février 2016

How to unit test database functions? (Doctrine2)

I may dont understand the concept of testing. Here is a function:

$qb->select('u')
   ->from('User', 'u')
   ->where('u.id = ?1')
   ->orderBy('u.name', 'ASC');

it is then "very" easy to write a mock, replace, expect the parameters, expect something result, etc. This is also detached from the database (no need real database then).

But what if I miss the ordering? I can still write a good test for it, without making sure it actually works. Another example:

function add($a, $b)
{
    return $a-$b;
}

our imaginary test:

function testAdd()
{
    $a = 1;
    $b = 4;

    $mock = $this->getMock(test);
    $mock->expects($a)->once()->with(1);
    $mock->expects($b)->once()->with(4);

    $this->assertEquals (-4324,534, testAdd($a, $b);
}

this (fictional) test is similar to database test above. Lets take some parameters, expect that they run once, and produces a fake value. But actually, I still dont know it my "Add" method works well.

Aucun commentaire:

Enregistrer un commentaire