lundi 30 avril 2018

PHP unit test, mock entity and return null to throw exception?

The method which I need to test is:

getItemsByUserAccount(UserAccount $userAccount) {

    if (!$userAccount) {
        throw new \Exception('something went wrong');
    }

}

in my test I try to test it so:

testGetItemsByUserAccount() {

    $this->expectException(\Exception::class);

    $call = new ElementsController();
    $call->getItemsByUserAccount($userAccount)
}

The problem is, how to mock the Entity UserAccount and return null to get this exception?

I tried with $this->getMockBuilder(UserAccount::class), but I don't now how to return null here without method...

Aucun commentaire:

Enregistrer un commentaire