jeudi 9 avril 2015

Unit test not triggering try/catch statement

The test for the class below fails with the following error.



Method error() from Mockery_15_Illuminate_Log_Writer should be called exactly 1 times but called 0 times.


I am trying to assert the ModelNotFoundException is thrown and the code in the catch section is being run. It seems the exception is being thrown correctly but for some reason it stops there. I couldn't find anything in the docs about this but this is my first time testing try/catch so I may just be missing something.


Thanks in advance. Let me know if you need more info.


NOTE: $this->userRepo is a mocked object being injected through the constructor. In case that wasn't clear.


Class:



public function fire()
{
try{

//if the userEmail option is specified, then only run that email for a specific user
if($this->option('userEmail')) {

// the point the exception is thrown
$user = $this->userRepo->findBy('email', $this->option('userEmail'));

// other non-important code

} else {

// other code

}

} catch(ModelNotFoundException $e) {
// the code that is not being run even when exception is thrown
$message = 'User with email of '. $this->option('userEmail'). ' does not exist in '. App::environment(). ' db';
\Log::error($message);
$this->error($message);

} catch(Exception $e) {

\Log::error($e->getMessage());
$this->error($e->getMessage());
}
}


Test:



public function email_reports_fail_to_send_if_no_model_found()
{
$this->setExpectedException('ModelNotFoundException');
$this->userRepo
->shouldReceive('findBy')
->once()
->with('email', 'whatAMadeUpEmailThisIsRidiculous@gmail.com')
->andThrow('ModelNotFoundException');

\Log::shouldReceive('error')->once();

$this->commandTester->execute(['command' => $this->command->getName(), '--userEmail' => 'whatAMadeUpEmailThisIsRidiculous@gmail.com']);
}

Aucun commentaire:

Enregistrer un commentaire