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

只谈情不闲聊 提交于 2019-12-11 03:15:00

问题


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:


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



来源:https://stackoverflow.com/questions/31921451/laravel-5-1-phpunit-api-test-returns-always-invalid-argument-error-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!