Laravel 5.1 + PHPunit - API test returns always in

2019-07-08 10:07发布

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('/srv/plc/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:

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

$this->visit works fine.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-08 10:48

After a lot of debugging....

  • seeJson() only accepts an Json array (not Json object)
  • Foreach error appears when the tested endpoint not returns an array. If there's more than an array, the error appears.

I really don't know why seeJson must be an array.

I expected an 'assertion error', instead of an foreach error

查看更多
登录 后发表回答