fahad's avatar
Level 7

Mock Error in PhpUnit test

I am always getting error while writing a simple test, don't know what's wrong there, any sort of help is highly appreciated.

Method pluck(<Any Arguments>) from Mockery_6_App_Models_EmailList should be called
 exactly 1 times but called 0 times

My Method is as below:


public function getDoNotEmailers(EmailList $list)
{
   return $list->pluck('email');
} 

and My test

class getDoNotEmailersTest extends TestCase{

  public function testGetDoNotEmail()
  {
     $mock = Mockery::mock('App\Models\EmailList');

     $mock->shouldReceive('pluck')->once()->andReturn('abracadebra'):

     $list = new EmailList(['email' => '[email protected]'])
     $actual = $this->sut->getDoNotEmailers($list):
 
  }
}


0 likes
3 replies
Talinon's avatar
Talinon
Best Answer
Level 51

You are mocking EmailList, but still end up instantiating an object and passing it to your function. This ends up calling pluck() on an actual instance of EmailList, and not your mock.

Have you tried passing your mock instead?

 $actual = $this->sut->getDoNotEmailers($mock):

Please or to participate in this conversation.