Question

I am trying to use Mockery to determine if my controller was properly called.

I make the call to the function from within my test case and the method returns properly. However, Mockery doesn't seem to catch that call.

I tried doing the call using both $this->call and $this->client->request. Both calls return the result, so Mockery should count the call to the controller.

public function testIndex()
{

  /**$entity = \Entity\Classes\Entity::get();
  var_dump($entity);    **/ 
  //This works, and is returning all the entities for that entity

  $headers = array();

  $mock = Mockery::mock('\Entity\Classes\Entity');

  $mock->shouldReceive('index')->once();

  $crawler = $this->custom_request('GET', '/entity/entities/114', $headers); 

  //echo $response = $this->client->getResponse()->getContent();
  //This also works, so the call is being made. custom_request calls $this->client->request method 

  //$this->call('GET', 'http://myurl:1000/entity/entities/114');
      //This alternate method to make the call also work

  $this->assertResponseOk();

}

Error:

1) ClassTest::testIndex
Mockery\Exception\InvalidCountException: Method index() from       
Mockery_0_Entity_Classes_Entity should be called
 exactly 1 times but called 0 times.
Was it helpful?

Solution

Usually you'd be injecting the mock into something, right now it's just sitting around in your test method and not being used. If you're using Laravel, you either need to be replacing the actual Entity\Classes\Entity in the IoC container with the mock, or if Entity\Classes\Entity::index is a static method, you need to use alias mocking, but I wouldn't recommend that, it's a can of worms.

Edit:

Search for "alias:" on this page https://github.com/padraic/mockery/blob/master/docs/05-QUICK-REFERENCE.md for alias mocking. Note you will probably want to run tests that use alias mocking with phpunit's process isolation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top