简体   繁体   English

Laravel-单元测试

[英]Laravel - unit testing

Hi i am trying to set aa test to test my database calls. 嗨,我正在尝试设置一个测试来测试我的数据库调用。 I want to pass a variables in url but cant seem to get it to work. 我想在url中传递变量,但似乎无法使其正常工作。

I wanted to do this 我想这样做

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

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

But i keep getting "Undefined variable : keyword" when i try. 但是,当我尝试时,我不断收到“未定义的变量:关键字”。 It works in the browser just not when i run phpunit. 它在浏览器中工作,只是当我运行phpunit时不行。 Anyone got any ideas on why this is not working thanks. 任何人都对为什么它不起作用有任何想法,谢谢。

The answer here is you need to specify the parameters differently in your call method: 答案是您需要在call方法中以不同的方式指定参数:

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

Below is the implementation of Illuminate\\Foundation\\Testing\\TestCase::call method: 下面是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();
}

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

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