symfony - PHPUnit - Symfony2 : Method was expected to be called 1 times, actually called 0 times -


i struggling mock. try tests on website (symfony2). here error :

there 1 failure:  1) l3l2\entraidebundle\tests\controller\inboxcontrollertest::testgetnbtotalmessagepasdejavu expectation failed method name equal <string:getnbtotalmessagepasdejavu> when invoked 1 time(s). method expected called 1 times, called 0 times. 

i saw few examples on internet argument $entitymanager attribute of class (but have no time change anymore). hope it's not origin of problem... here code :

inboxcontroller :

public function getnbtotalmessagepasdejavu(objectmanager $entitymanager = null) {     if($entitymanager == null)         $entitymanager = $this->getdoctrine()->getentitymanager();      $repository = $entitymanager->getrepository('l3l2entraidebundle:message');      $nbmessagepasdejavu = $repository->getnbtotalmessagepasdejavu(1);       //change 1 $this->getuser()->getid()      return $nbmessagepasdejavu; } 

inboxcontrollertest :

class inboxcontrollertest extends \phpunit_framework_testcase {     public function testgetnbtotalmessagepasdejavu()     {         $msgrepository = $this             ->getmockbuilder("\doctrine\orm\entityrepository")             ->disableoriginalconstructor()             ->getmock();          $msgrepository->expects($this->once())             ->method('getnbtotalmessagepasdejavu')             ->will($this->returnvalue(0));           $entitymanager = $this->getmockbuilder('\doctrine\common\persistence\objectmanager')             ->disableoriginalconstructor()             ->getmock();         $entitymanager->expects($this->once())             ->method('getrepository')             ->will($this->returnvalue($msgrepository));          $msg = new inboxcontroller();          $this->assertequals(0, $msg->getnbtotalmessagepasdejavu($entitymanager));     } } 

does have idea ? thank !

the problem in following line section:

$msgrepository->expects($this->once())     ->method('getnbtotalmessagepasdejavu')     ->will($this->returnvalue(0)); 

by declaring ->expects($this->once()) method called once per test case. if don't use it, trigger exception.

if don't need method triggered excactly once per test, use ->expects($this->any()) instead.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -