vendredi 22 juillet 2016

PHPUnit with Laravel, expected exception breaks Mockery expectations?

Controller code:

public function getIndex($resource) {
    $r = $this->mapper->map($resource);
    if (! $r) {
        throw new \Exception("Resource not found.");
    }
}

This is my test:

public function testGetIndex_if_resource_is_not_found() {
    $this->mapper->shouldReceive('map')->with('foo')->once()->andReturnNull();
    $this->setExpectedException('Exception');
    $this->call('GET', 'resources/foo');
}

I get this:

Mockery\Exception\InvalidCountException: Method map("foo") from Mockery_0_Foothing_RepositoryController_Resources_Mapper should be called
 exactly 1 times but called 0 times.

If i disable the expectation on the exception like this:

public function testGetIndex_if_resource_is_not_found() {
    $this->mapper->shouldReceive('map')->with('foo')->once()->andReturnNull();
    // $this->setExpectedException('Exception');
    $this->call('GET', 'resources/foo');
}

the test output will be

Exception: Resource not found.

Seems like i can't having both expectations to work :/ What's broken in my code?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire