laravel - Unit test not triggering try/catch statement -
the test class below fails following error.
method error() mockery_15_illuminate_log_writer should called 1 times called 0 times.
i trying assert modelnotfoundexception thrown , code in catch section being run. seems exception being thrown correctly reason stops there. couldn't find in docs first time testing try/catch may missing something.
thanks in advance. let me know if need more info.
note: $this->userrepo mocked object being injected through constructor. in case wasn't clear.
class:
public function fire() { try{ //if useremail option specified, run email specific user if($this->option('useremail')) { // point exception thrown $user = $this->userrepo->findby('email', $this->option('useremail')); // other non-important code } else { // other code } } catch(modelnotfoundexception $e) { // code not being run when exception thrown $message = 'user email of '. $this->option('useremail'). ' 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']); }
when configuring mock throwing exception, must refer exception class using qualified name, i.e. including namespace. if don't class won't same , exception catched default catch.