【问题标题】:Laravel Write Update TestLaravel 写更新测试
【发布时间】:2020-09-23 20:22:58
【问题描述】:

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

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

        $this->actingAs($this->user)
            ->put('/contacts/' . Contact::first()->id, ['first_name' => 'bablo']);
        // TRIED PATCH TOO
        // TRIED '/contacts' AS WELL


        dd(Contact::first());
    }

提取的联系人未更新。有什么建议吗?

编辑

我从请求中收到以下错误

 +response: null
  +status: 422
  +errorBag: "default"
  +redirectTo: null
  #message: "The given data was invalid."
  #code: 0
  #file: "C:\xampp\htdocs\myhobbies\vendor\laravel\framework\src\Illuminate\Validation\Validator.php"
  #line: 452
  trace: {
    C:\xampp\htdocs\myhobbies\vendor\laravel\framework\src\Illuminate\Validation\Validator.php:452 { …}
    C:\xampp\htdocs\myhobbies\vendor\laravel\framework\src\Illuminate\Validation\Factory.php:136 { …}
    C:\xampp\htdocs\myhobbies\vendor\laravel\framework\src\Illuminate\Foundation\Providers\FoundationServiceProvider.php:58 { …}
    Illuminate\Http\Request->Illuminate\Foundation\Providers\{closure}() {}
    C:\xampp\htdocs\myhobbies\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php:111 { …}
    C:\xampp\htdocs\myhobbies\app\Http\Controllers\ContactsController.php:128 {
      App\Http\Controllers\ContactsController->validateData()
      › return request()->validate([\r
      ›     'first_name' => ['required', 'max:50'],\r
      ›     'last_name' => ['required', 'max:50'],\r
      arguments: {
        $method: "validate"
        $parameters: array:1 [ …1]
      }
    }
    C:\xampp\htdocs\myhobbies\app\Http\Controllers\ContactsController.php:103 { …}
    App\Http\Controllers\ContactsController->update() {}

【问题讨论】:

  • 你尝试过什么调试问题?
  • github.com/inertiajs/pingcrm/blob/master/app/Http/Controllers/… 基于此,您缺少一些必填字段。
  • @NicoHaase 使用了 dd()
  • @ClémentBaconnier 现在我也试过了 array_merge(Contact::first()->toArray(), ['first_name' => 'bablo']) 但还是不行
  • 但是 API 中的更新有效

标签: laravel testing


【解决方案1】:

根据source,您缺少一些必填字段

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

【讨论】:

  • 尝试在测试的第一行添加$this->withoutExceptionHandling();
  • 我相信你也可以通过类似dd(session('errors')->all())
  • 好的问题是名字和姓氏是必需的,还是不明白为什么array_merge不起作用,可能是因为我试图强制id?
  • 可能是假数据也不遵守验证规则。
猜你喜欢
  • 2018-10-27
  • 1970-01-01
  • 2020-03-09
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 2012-03-02
相关资源
最近更新 更多