Laravel 5.1 + PHPunit - API 测试 returns 始终无效参数错误 foreach

Laravel 5.1 + PHPunit - API test returns always invalid argument error foreach

我已经从 Laravel 5.0 升级到 5.1

测试套件工作正常,我可以 运行 phpunit 命令。但是,当我开始使用 api 测试进行测试时,我总是会遇到 foreach 错误。

class ExampleTest extends TestCase {

    public function testLoginCredentials()
    {
        $this->post('/srv/plc/auth/login', ['data' => 'some data'])
        ->seeJson([
            'authorized' => true,
        ]);
    }
}

上面看起来像文档:http://laravel.com/docs/5.1/testing#testing-json-apis

如果我 运行 通过 phpunit 进行测试,我会收到以下错误:

There was 1 error:

1) ExampleTest::testBasicExample
ErrorException: Invalid argument supplied for foreach()
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Support/Arr.php:423
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Support/helpers.php:301
/Applications/XAMPP/xamppfiles/htdocs/w/server/vendor/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:365
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php:352
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/tests/ExampleTest.php:17
/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/TextUI/Command.php:188
/Applications/XAMPP/xamppfiles/lib/php/PHPUnit/TextUI/Command.php:126

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

如果我用 $this->get 做一个 get 请求,我会得到同样的错误。与其他端点相同的错误。

$this->访问正常。

经过大量调试....

  • seeJson() 只接受一个 Json 数组(不是 Json 对象)
  • 当测试的端点不是returns数组时出现Foreach错误。如果不止一个数组,就会出现错误。

真不知道为什么看到Json一定是数组

我预计会出现 'assertion error',而不是 foreach 错误