简体   繁体   中英

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.

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. 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:

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

Below is the implementation of Illuminate\\Foundation\\Testing\\TestCase::call method:

/**
 * 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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