Laravel 5.2 - 为 foreach() 提供的参数无效 - 使用 phpunit
Laravel 5.2 - Invalid argument supplied for foreach() - using phpunit
我在使用 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);
}
完整的错误报告在这里:
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.
我承认这段代码不是我的原创 - 它是从 Laravel 教程中复制的。它在那里工作得很好。
不幸的是,这个相关问题的答案也没有帮助我。
我试图修改它以便将 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);
}
但后来我得到了这个错误:
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
此错误告诉您您在此处传递了错误的类型:
->seeJson(json_encode(array('name' => ['The name field is required.'])))
您必须将其更改为如下所示,然后它应该可以正常工作。
->seeJson(array('name' => ['The name field is required.']))
我在使用 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);
}
完整的错误报告在这里:
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.
我承认这段代码不是我的原创 - 它是从 Laravel 教程中复制的。它在那里工作得很好。
不幸的是,这个相关问题的答案也没有帮助我。
我试图修改它以便将 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);
}
但后来我得到了这个错误:
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
此错误告诉您您在此处传递了错误的类型:
->seeJson(json_encode(array('name' => ['The name field is required.'])))
您必须将其更改为如下所示,然后它应该可以正常工作。
->seeJson(array('name' => ['The name field is required.']))