简体   繁体   English

嘲笑似乎无法正常工作

[英]Mockery doesn't seem to be working properly

I am trying to use Mockery to determine if my controller was properly called. 我正在尝试使用Mockery来确定是否正确调用了我的控制器。

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. 但是,Mockery似乎没有接听电话。

I tried doing the call using both $this->call and $this->client->request. 我尝试使用$ this-> call和$ this-> client-> request进行呼叫。 Both calls return the result, so Mockery should count the call to the controller. 这两个调用都返回结果,因此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();

}

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.

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. 如果您使用Laravel,则需要用模拟代替IoC容器中的实际Entity\\Classes\\Entity ,或者如果Entity\\Classes\\Entity::index是静态方法,则需要使用别名模拟,但我不建议这样做,它是一罐蠕虫。

Edit: 编辑:

Search for "alias:" on this page https://github.com/padraic/mockery/blob/master/docs/05-QUICK-REFERENCE.md for alias mocking. 在此页面上搜索“别名:” https://github.com/padraic/mockery/blob/master/docs/05-QUICK-REFERENCE.md以进行别名模拟。 Note you will probably want to run tests that use alias mocking with phpunit's process isolation. 注意,您可能需要运行将别名模拟与phpunit的进程隔离一起使用的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM