【问题标题】:Mockery doesn't seem to be working properly嘲弄似乎无法正常工作
【发布时间】:2014-03-24 17:12:43
【问题描述】:

我正在尝试使用 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.

【问题讨论】:

    标签: php laravel phpunit mockery


    【解决方案1】:

    通常您会将模拟注入到某些东西中,现在它只是在您的测试方法中闲置而不被使用。如果您使用 Laravel,则需要将 IoC 容器中的实际 Entity\Classes\Entity 替换为模拟,或者如果 Entity\Classes\Entity::index 是静态方法,则需要使用别名模拟,但我不建议这样做,这是一罐虫子。

    编辑:

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

    【讨论】:

    • 啊。当我发布这个时,我正在调查一些事情,我也看到了你的回复。事实证明,我确实需要使用别名模拟,因为一切都是静态的。你能具体说明一下别名嘲笑吗?我在项目的 github 上找不到任何重要的东西。
    • @Dynelight 再次查看答案,添加链接
    • 戴夫最后一个问题:过滤器是否应该使用它来解决问题?我尝试添加别名,但我得到了一个“BadMethodCallException:方法实体\控制器\实体::getAfterFilters()在这个模拟对象上不存在”......你的意思是蠕虫罐头吗?
    • 另外,当我通过 GET 调用我正在模拟的控制器时,不应该在那里调用模拟吗?
    • @Dynelight 恐怕我不确定,我不太了解 eloquent(或你的代码)
    猜你喜欢
    • 2013-04-08
    • 2017-09-04
    • 2012-05-06
    • 2013-02-17
    • 2017-10-09
    • 2017-08-03
    • 2014-05-02
    • 2011-09-11
    • 2016-02-11
    相关资源
    最近更新 更多