php - Laravel5 中的单元测试 - 错误

标签 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()

为了传递 header ,我将它们设置在 $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 和一个空数组,但错误从未解决。大家有什么想法吗?

最佳答案

我在 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]);

关于php - Laravel5 中的单元测试 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30262240/

相关文章:

php - Laravel 5.0 用户 Eloquent 模型 mock

unit-testing - 从 FakeItEasy A.CallTo() 返回不同的对象

php - 如何在 phpunit 测试运行时禁用在控制台上显示应用程序日志消息?

php - Laravel 验证 Controller 存储中的变量

php - WordPress <?php the_post_thumbnail( 'full' ); ?> 没有 img 标签

c# - XNA 模拟游戏对象或解耦你的游戏

unit-testing - 验证单元测试实际上正在运行,而不是因为写得不好而跳过

PHPUnit 和 C.R.A.P 索引

PHP CSV 文件生成,如何更改 csv 列宽

javascript - 使用 AJAX PHP 显示数据