Laravel 5.2 – 为foreach()提供的参数无效 – 使用phpunit

Laravel 5.2 – 为foreach()提供的参数无效 – 使用phpunit,第1张

概述使用phpunit命令测试我的应用程序时出现上述错误. public function testProductCreationFailsWhenNameNotProvided(){ $product = factory(\App\Product::class)->make(['name' => '']); $this->post(route('api.products.stor 使用PHPunit命令测试我的应用程序时出现上述错误.

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);}

完整的错误报告在这里:

There was 1 error:1) ExampleTest::testProductCreationFailsWhennameNotProvIDedErrorException: InvalID argument supplIEd for foreach()C:\xampp\htdocs\product-  service\vendor\laravel\framework\src\Illuminate\Support\Arr.PHP:494C:\xampp\htdocs\product-service\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakeshttpRequests.PHP:231C:\xampp\htdocs\product-service\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakeshttpRequests.PHP:257C:\xampp\htdocs\product-service\tests\ExampleTest.PHP:86C:\xampp\PHP\pear\PHPUnit\TextUI\Command.PHP:176C:\xampp\PHP\pear\PHPUnit\TextUI\Command.PHP:129FAILURES!Tests: 7,Assertions: 43,Errors: 1.

我承认这段代码原本不是我的 – 它是从Laravel教程复制的.它在那里工作得很好.
不幸的是,这个相关问题的答案也没有帮助我.
Laravel 5.1 + PHPunit – API test returns always invalid argument error foreach

我试图修改它以传递Json数组作为参数

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);    }

但后来我收到了这个错误:

1) ExampleTest::testProductCreationFailsWhennameNotProvIDedTypeError: 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) ExampleTest::testProductCreationFailsWhennameNotProvIDed
TypeError: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::seeJson() must be of the type array,called in C:\xampp\htdocs\product-service\tests\ExampleTest.PHP on line 86

此错误告诉您在此处传递了错误的类型:

->seeJson(Json_encode(array('name' => ['The name fIEld is required.'])))

你必须改变它看起来像这样,它应该工作.

->seeJson(array('name' => ['The name fIEld is required.']))
总结

以上是内存溢出为你收集整理的Laravel 5.2 – 为foreach()提供的参数无效 – 使用phpunit全部内容,希望文章能够帮你解决Laravel 5.2 – 为foreach()提供的参数无效 – 使用phpunit所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1251547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存