Laravel5中的单元测试 - 错误

时间:2015-05-15 14:36:24

标签: php unit-testing phpunit laravel-5

根据:

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

为了传递标题,我将它们设置在$server参数中,我这样做:

public function testCreateLocation(){
  $response = $this->call('POST', '/api/v1/token', $this->test_token_request);
  $tokenObject = json_decode($response->getContent());

  /** Run the test */
  $response = $this->call('POST', '/api/v1/location', $this->test_1_create_tag,
    [], ['Authorization' => 'Bearer '.$tokenObject->token]);

  /** Read the response */
  $this->assertResponseOk();
}

但是,当我运行单元测试时,我收到以下错误:

vagrant@homestead:~/Code$ php phpunit.phar tests/LocationModelTest.php
PHPUnit 4.6.2 by Sebastian Bergmann and contributors.

Configuration read from /home/vagrant/Code/phpunit.xml.dist

EFF

Time: 3.75 seconds, Memory: 35.75Mb

There was 1 error:

1) LocationModelTest::testCreateLocation
InvalidArgumentException: An uploaded file must be an array or an instance of UploadedFile.

我已尝试传递null和一个空数组,但错误从未得到解决。有人有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我也在Laravel 5.1上遇到过这个问题。一旦我检查了最新documentation on the call() method,我就看到了这个问题:

call(string $method, string $uri, array $parameters = array(), **array $cookies = array(),** array $files = array(), array $server = array(), string $content = null)

在此过程中的某个地方,一个额外的参数(cookies)被添加到方法中。

所以添加一个额外的空数组,你应该很好:

$response = $this->call('POST', '/api/v1/location', $this->test_1_create_tag, [], [], ['Authorization' => 'Bearer '.$tokenObject->token]);