2

我正在尝试使用 Mockery 来确定我的控制器是否被正确调用。

我从我的测试用例中调用该函数并且该方法正确返回。然而,嘲弄似乎没有抓住那个电话。

我尝试使用 $this->call 和 $this->client->request 进行通话。两个调用都返回结果,所以 Mockery 应该计算对控制器的调用。

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();

}

错误:

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

1 回答 1

2

通常你会将模拟注入到某些东西中,现在它只是坐在你的测试方法中并且没有被使用。如果您使用 Laravel,则需要将Entity\Classes\EntityIoC 容器中的实际内容替换为 mock,或者如果Entity\Classes\Entity::index是静态方法,则需要使用别名 mocking,但我不建议这样做,这是一罐蠕虫.

编辑:

在此页面https://github.com/padraic/mockery/blob/master/docs/05-QUICK-REFERENCE.md上搜索“别名:”以进行别名模拟。请注意,您可能希望运行使用别名模拟和 phpunit 进程隔离的测试。

于 2014-03-24T19:48:04.857 回答