[英]Laravel 5.2 - Invalid argument supplied for foreach() - using phpunit
I get the above error when testing my application with phpunit command. 使用phpunit命令测试我的应用程序时出现上述错误。
public function testProductCreationFailsWhenNameNotProvided()
{
$product = factory(\App\Product::class)->make(['name' => '']);
$this->post(route('api.products.store'), $product->jsonSerialize())
->seeJson(['name' => ['The name field is required.']]) /*line 86*/
->assertResponseStatus(422);
}
The full error report is here: 完整的错误报告在这里:
There was 1 error:
1) ExampleTest::testProductCreationFailsWhenNameNotProvided
ErrorException: Invalid argument supplied for foreach()
C:\xampp\htdocs\product- service\vendor\laravel\framework\src\Illuminate\Support\Arr.php:494
C:\xampp\htdocs\product-service\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:231
C:\xampp\htdocs\product-service\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:257
C:\xampp\htdocs\product-service\tests\ExampleTest.php:86
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
FAILURES!
Tests: 7, Assertions: 43, Errors: 1.
I confess that this code is not originally mine - it is copied from a Laravel tutorial. 我承认这段代码原本不是我的 - 它是从Laravel教程复制的。 It worked fine there.
它在那里工作得很好。 Unfortunately the answer to this related question did not help me either.
不幸的是,这个相关问题的答案也没有帮助我。 Laravel 5.1 + PHPunit - API test returns always invalid argument error foreach
Laravel 5.1 + PHPunit - API测试返回始终无效的参数错误foreach
I tried to modify it in order to pass an json array as a parameter 我试图修改它以传递json数组作为参数
public function testProductCreationFailsWhenNameNotProvided()
{
$product = factory(\App\Product::class)->make(['name' => '']);
$this->post(route('api.products.store'), $product->jsonSerialize())
->seeJson(json_encode(array('name' => ['The name field is required.'])))
->assertResponseStatus(422);
}
but then I got this error: 但后来我收到了这个错误:
1) ExampleTest::testProductCreationFailsWhenNameNotProvided
TypeError: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::seeJson() must be of the type array, string given, called in C:\xampp\htdocs\product-service\tests\ExampleTest.php on line 86
1) ExampleTest::testProductCreationFailsWhenNameNotProvided TypeError: Argument 1 passed to Illuminate\\Foundation\\Testing\\TestCase::seeJson() must be of the type array, string given, called in C:\\xampp\\htdocs\\product-service\\tests\\ExampleTest.php on line 86
1)ExampleTest :: testProductCreationFailsWhenNameNotProvided TypeError:传递给Illuminate \\ Foundation \\ Testing \\ TestCase :: seeJson()的参数1必须是类型数组,给定字符串,在C:\\ xampp \\ htdocs \\ product-service \\ tests \\ ExampleTest中调用第86行的.php
This error tells you that you passed wrong type here: 此错误告诉您在此处传递了错误的类型:
->seeJson(json_encode(array('name' => ['The name field is required.'])))
You have to change it to look like this and it should work then. 你必须改变它看起来像这样,它应该工作。
->seeJson(array('name' => ['The name field is required.']))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.