Juliatzin
Juliatzin

Reputation: 19705

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" => "[email protected]"
    "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 ?

Upvotes: 1

Views: 276

Answers (1)

cdruc
cdruc

Reputation: 637

From the looks of it your validation expects different parameters than you are submitting: You need to send company, not company_id, operation, not operation_id, and so on.

This should work:

$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
]);

Upvotes: 1

Related Questions