4
votes

I get the above error when testing my application with 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.']]) /*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. 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

I tried to modify it in order to pass an 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
1
Did you change anything in your code based on the SO question you link to, because you're making the same mistake there that the poster of that question was making - passing an object rather than an array into seeJson(). - tex
yes, I actually tried various combinations like -seeJson(json_encode(array('name' => ['The name field is required.']))) and a lot more - Andrei
However, the official documentation says that the initial version was correct laravel.com/docs/5.1/testing#testing-json-apis - Andrei
Just remove the [] from 'The name field is required.' What response is the controller answering? - jackomo

1 Answers

1
votes

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

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.']))