I am getting the above error when testing my application using phpunit command.
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.']])
->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 admit that this code was not originally mine - it was copied from the Laravel tutorial. He worked great there. Unfortunately, the answer to this related question did not help me either.
Laravel 5.1 + PHPunit - API test always returns an incorrect foreach argument error
I tried changing it to pass a json array as a parameter
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
source
share