2014-03-24 75 views
1

我想用嘲笑來確定我的控制器是否被正確調用。嘲笑似乎沒有正常工作

我在我的測試用例中調用函數,並且方法正確返回。但是,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. 

回答

2

通常你會注入模擬到的東西,現在它只是圍坐在您的測試方法,並沒有被使用。如果您使用的是Laravel,您需要使用模擬替換IoC容器中的實際Entity\Classes\Entity,或者如果Entity\Classes\Entity::index是靜態方法,則需要使用別名模擬,但我不會推薦這是一個罐的蠕蟲。

編輯:

搜索 「別名:」 此頁https://github.com/padraic/mockery/blob/master/docs/05-QUICK-REFERENCE.md別名嘲弄上。請注意,您可能會想要運行使用別名模擬與phpunit進程隔離的測試。

+0

啊。當我發佈這篇文章時,我正在研究一些事情,並且我也遇到了你的迴應。事實證明,我確實需要使用別名嘲笑,因爲一切都是靜態的。你能指出我有點別名嘲弄嗎?我找不到任何重要的gye項目。 – Dynelight

+0

@Dynelight再次看到答案,添加鏈接 –

+0

Dave最後一個問題:過濾器是否應該使用這個問題?我嘗試添加別名,並且在這個模擬對象上沒有存在'BadMethodCallException:Method Entity \ Controllers \ Entity :: getAfterFilters()'...是您的意思嗎? – Dynelight