簡體   English   中英

Laravel Nova 上的 belongsTo 驗證錯誤,但字段在測試中具有值

[英]Validation error on a belongsTo on Laravel Nova, but fields has a value in testing

我正在編寫一個小測試

/** @test **/
public function password_can_be_nullable_on_update()
{
    $user = factory(\App\User::class)->make([
        'password' => null,
    ]);
    $response = $this->put('/nova-api/users/'.$this->user->id,
            $user->getAttributes()
    );
    $response->assertStatus(200);
}

當我調試我的 $user object 時,我可以看到工廠正在填寫company_idoperation_idprofile_id

#attributes: array:10 [
    "name" => "Lilian Crona"
    "operation_id" => 1
    "email" => "jessika15@example.org"
    "email_verified_at" => "2020-08-13 15:41:31"
    "password" => null
    "remember_token" => "tiS2N28USF"
    "company_id" => 1
    "profile_id" => 3
    "created_at" => "2020-08-13 15:41:31"
    "updated_at" => "2020-08-13 15:41:31"
  ]

所以,我想 object 是完整的,但是當我運行測試時,我在 3 個字段上收到了驗證錯誤。

#bags: array:1 [
      "default" => Illuminate\Support\MessageBag]8;;file:///home/julien/Code/acc/vendor/laravel/framework/src/Illuminate/Support/MessageBag.php#L12\^]8;;\ {#3857
        #messages: array:3 [
          "company" => array:1 [
            0 => "Le champ company est obligatoire."
          ]
          "operation" => array:1 [
            0 => "Le champ operation est obligatoire."
          ]
          "profile" => array:1 [
            0 => "Le champ profile est obligatoire."
          ]

一種解決方法是:

   $user = factory(\App\User::class)->make([
            'password' => null,
            'company' => factory(Company::class)->create(),
            'operation' => factory(Operation::class)->create(),
            'profile' => factory(Profile::class)->create(),
        ]);

但我不知道為什么會這樣?

從外觀上看,您的驗證需要與您提交的參數不同的參數:您需要發送company ,而不是company_idoperation ,而不是operation_id ,等等。

這應該有效:

$response = $this->put('/nova-api/users/' . $this->user->id, [
    "name" => $user->name,
    "operation" => $user->operation_id,
    "email" => $user->email,
    "password" => null
    "company" => $user->company_id,
    "profile" => $user->profile_i
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM