jeudi 23 juillet 2020

Laravel assertDatabaseHas in phpunit test is not working

I have the following code:

/** @test */
    public function it_updates_customer_status_to_deactivated_for_admin_users()
    {
        $this->hubAdminUser = factory(User::class)->state('admin')->create();
        $this->customer = Customer::first();
        $this->customer->status_id = 2; //active
        $this->customer->save();

        // this will update status_id to 3
        $this->actingAs($this->hubAdminUser)
            ->patch(route('hub.customer.updateStatus', $this->customer))
            ->assertRedirect();

        $this->assertDatabaseHas('tenants', [
            'id' => $this->customer->id,
            'status_id' => 3, //deactivated
        ]);
    }

The ->patch(route('hub.customer.updateStatus', $this->customer)) line will change the value of status_id from 2 to 3 which it definitely does as I have even tried $this->customer->refresh()->status_id after the ->assertRedirect(); line and that gives me 3. This is failing as it says that the customer's status_id is set to 2 in the database. Any ideas how I can fix this?

Aucun commentaire:

Enregistrer un commentaire