jeudi 13 août 2020

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

I am coding a small test

/** @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);
    }

when I debug my $user object, I can see factory is filling company_id, operation_id, profile_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"
  ]

So, I would imagine object is complete, but when I run the test,I get a validation error on the 3 fields.

#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."
          ]

a workaround is to do:

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

but I don't know why is it happening ?

Aucun commentaire:

Enregistrer un commentaire