Laravel 5.1 + PHPunit - API 测试总是返回无效参数错误 foreach [英] Laravel 5.1 + PHPunit - API test returns always invalid argument error foreach
问题描述
我已经从 Laravel 5.0 升级到 5.1
I've upgraded from Laravel 5.0 to 5.1
测试套件运行良好,我可以运行 phpunit 命令.但是,当我开始使用 api 测试进行测试时,总是出现 foreach 错误.
Test suite works fine and I can run the phpunit command. But, when I'm start to test with the api test, I always get a foreach error.
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 运行我的测试,我会收到以下错误:
If I run my test via phpunit, I get the following error:
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 请求,我会得到同样的错误.与其他端点相同的错误.
If I do a get request with $this->get, I get the same error. Same error with other endpoints.
$this->visit 工作正常.
$this->visit works fine.
推荐答案
经过大量调试....
- seeJson() 只接受一个 Json 数组(不是 Json 对象)
- 当测试的端点未返回数组时,会出现 Foreach 错误.如果有多个数组,则会出现错误.
我真的不知道为什么 seeJson 必须是一个数组.
I really don't know why seeJson must be an array.
我预计会出现断言错误",而不是 foreach 错误
I expected an 'assertion error', instead of an foreach error
这篇关于Laravel 5.1 + PHPunit - API 测试总是返回无效参数错误 foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!