Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel写更新测试_Laravel_Testing - Fatal编程技术网

Laravel写更新测试

Laravel写更新测试,laravel,testing,Laravel,Testing,我从克隆的pingcrm开始使用Laravel8,并尝试编写一个更新联系人的测试,但似乎不起作用。 请注意,我更改了预设结构,现在联系人直接关联到创建它的用户(而不是帐户) 公共功能测试\u授权用户\u可以\u更新\u联系人() { $this->user->contacts()->saveMany( 工厂(Contact::class,5)->make() ); $this->actingAs($this->user) ->put('/contacts/'.Contact::first()-

我从克隆的pingcrm开始使用Laravel8,并尝试编写一个更新联系人的测试,但似乎不起作用。 请注意,我更改了预设结构,现在联系人直接关联到创建它的用户(而不是帐户)

公共功能测试\u授权用户\u可以\u更新\u联系人()
{
$this->user->contacts()->saveMany(
工厂(Contact::class,5)->make()
);
$this->actingAs($this->user)
->put('/contacts/'.Contact::first()->id,['first_name'=>'bablo']);
//我也试过补丁
//也尝试了“/联系人”
dd(Contact::first());
}
基于此,您缺少一些必填字段

public function update(Contact $contact)
    {
        $contact->update(
            Request::validate([
                'first_name' => ['required', 'max:50'],
                'last_name' => ['required', 'max:50'],
                'organization_id' => ['nullable', Rule::exists('organizations', 'id')->where(function ($query) {
                    $query->where('account_id', Auth::user()->account_id);
                })],
                'email' => ['nullable', 'max:50', 'email'],
                'phone' => ['nullable', 'max:50'],
                'address' => ['nullable', 'max:150'],
                'city' => ['nullable', 'max:50'],
                'region' => ['nullable', 'max:50'],
                'country' => ['nullable', 'max:2'],
                'postal_code' => ['nullable', 'max:25'],
            ])
        );

        return Redirect::back()->with('success', 'Contact updated.');
    }
您可以将其添加到测试中,以断言您没有任何错误

public function test_an_authorized_user_can_update_a_contact()
{
    // One possibility to see the detail of the error
    $this->withoutExceptionHandling(); 

    $this->user->contacts()->saveMany(
        factory(Contact::class, 5)->make()
    );

    $this->actingAs($this->user)
         ->put('/contacts/' . Contact::first()->id, ['first_name' => 'bablo'])
         // ->assertStatus(200)
         ;

     // second possibility to see the errors
     $errors = session('errors');
     dd($errors->all());
}

你试过调试这个问题吗?基于此,您缺少了一些必填字段。@NicoHaase使用了dd()@ClémentBaconnier现在我也尝试了数组合并(Contact::first()->toArray(),['first_name'=>'bablo']),但仍然不起作用,但API工作区中的更新尝试添加
$this->withoutExceptionHandling()在测试的第一行。我相信您也可以通过执行类似于
dd(session('errors')->all())的操作来获得错误。
确定问题是需要名字和姓氏,但仍然不理解为什么数组合并不起作用,可能是因为我想强制输入id?可能是伪造的数据也不遵守验证规则。