Henri85's avatar

Laravel 5.1 + PHPunit - API test returns always invalid argument error foreach

I've upgraded from Laravel 5.0 to 5.1

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('/server/public/auth/login', ['data' => 'some data'])
    ->seeJson([
        'authorized' => true,
    ]);
}

} Above looks like the documentation: http://laravel.com/docs/5.1/testing#testing-json-apis

If I run my test via phpunit, I get the following error:

There was 1 error:

ExampleTest::testBasicExample
ErrorException: Invalid argument supplied for foreach()
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/vendor/laravel/framework/src/Illuminate/Support/Arr.php:423
/Applications/XAMPP/xamppfiles/htdocs/whennn/server/vendor/laravel/framework/src/Illuminate/Support/helpers.php:301
 /Applications/XAMPP/xamppfiles/htdocs/whennn/server/vendor/laravel/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.

If I do a get request with $this->get, I get the same error. And same error with other endpoints.

$this->visit works fine.

0 likes
1 reply
Flambe's avatar

I've usually seen that error when the API wasn't returning JSON but I was expecting JSON, like you are.

Try adding ->dump() before the json check to see if you are actually getting JSON back.

1 like

Please or to participate in this conversation.