加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:40:06 所属栏目:大数据 来源:网络整理
导读:使用phpunit命令测试我的应用程序时出现上述错误. public function testProductCreationFailsWhenNameNotProvided(){ $product = factory(AppProduct::class)-make(['name' = '']); $this-post(route('api.products.store'),$product-jsonSerialize()) -se
使用phpunit命令测试我的应用程序时出现上述错误.

public function testProductCreationFailsWhenNameNotProvided()
{
    $product = factory(AppProduct::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::testProductCreationFailsWhenNameNotProvided
ErrorException: Invalid argument supplied for foreach()
C:xampphtdocsproduct-  servicevendorlaravelframeworksrcIlluminateSupportArr.php:494
C:xampphtdocsproduct-servicevendorlaravelframeworksrcIlluminateFoundationTestingConcernsMakesHttpRequests.php:231
C:xampphtdocsproduct-servicevendorlaravelframeworksrcIlluminateFoundationTestingConcernsMakesHttpRequests.php:257
C:xampphtdocsproduct-servicetestsExampleTest.php:86
C:xamppphppearPHPUnitTextUICommand.php:176
C:xamppphppearPHPUnitTextUICommand.php:129
FAILURES!
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(AppProduct::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::testProductCreationFailsWhenNameNotProvided
TypeError: Argument 1 passed to IlluminateFoundationTestingTestCase::seeJson() must be of the type array,string given,called in C:xampphtdocsproduct-servicetestsExampleTest.php on line 86

解决方法

1) ExampleTest::testProductCreationFailsWhenNameNotProvided
TypeError: Argument 1 passed to IlluminateFoundationTestingTestCase::seeJson() must be of the type array,called in C:xampphtdocsproduct-servicetestsExampleTest.php on line 86

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

->seeJson(json_encode(array('name' => ['The name field is required.'])))

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读