2014-03-26 52 views
0

嗨,我想設置一個測試來測試我的數據庫調用。我想通過在URL中的變量,但似乎無法讓它的工作。Laravel - 單元測試

我想做到這一點

public function testDb() 
{ 
       $response = $this->call('GET', '/searchAvailability?keyword=test product'); 
       $content = $response->getContent(); 

      $this->assertEquals('[{"name":"test product"}]',$content); 
} 

但一直收到「未定義的變量:關鍵字」當我嘗試。它運行在瀏覽器中,而不是當我運行phpunit時。任何人都有任何想法,爲什麼這不工作謝謝。

回答

1

這裏的答案是,你需要在你的call方法不同指定參數:

$this->call('GET', '/searchAvailability', array('keyword' => 'test product')); 

下面是Illuminate\Foundation\Testing\TestCase::call方法的實現:

/** 
* Call the given URI and return the Response. 
* 
* @param string $method 
* @param string $uri 
* @param array $parameters 
* @param array $files 
* @param array $server 
* @param string $content 
* @param bool $changeHistory 
* @return \Illuminate\Http\Response 
*/ 
public function call() 
{ 
    call_user_func_array(array($this->client, 'request'), func_get_args()); 

    return $this->client->getResponse(); 
}