I have a laravel project and I got a problem with using PhpUnit test use Mockery. This is the fist time I use Mockery.
Below, I have some pseudo code:
class Mail
{
public static function send($pin){
// Perform send mail
// This is fake current excetion
throw new exception('Send mail error');
}
}
class CompanyService
{
public function providePin($pin)
{
try {
// send mail but got exception
Mail::send($pin);
return true;
}catch(\Exception $e) {
// Write error to log file
return false;
}
}
}
I have a test class below and I tried to bypass the exception in Mail::send($pin) but It's not working.
// I have a test class
class CompanyTest
{
public function testProvidePin()
{
$mock = Mockery::mock(CompanyService::class)->makePartial();
$res = $mock->shouldReceive('providePin')->with(300386)->times(1)->andReturn(true);
// But everytime I got `false` at this
$this->assertTrue($res);
}
}
So, How can I bypass the exception in Mail::send($pin) ?
Thank for your help. 🙇🙇🙇🙇
Mail::send($pin)throw an exception, thenprovidePin()will return false.