4

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?

6
  • 1
    The table name for the Customer model is tenants? Commented Jul 23, 2020 at 13:53
  • Yes that's correct. It's part of this package: tenancyforlaravel.com/docs/v2/getting-started Commented Jul 23, 2020 at 13:59
  • How are your sure that this is will be working "// this will update status_id to 3"? I will be add assertStatus(200) on that, or if you are returning something I will be assert that to.. Commented Jul 23, 2020 at 14:03
  • 1
    There isn't enough information to really help you, the only thing that comes to mind is that assertDatabaseHas() might be using the wrong database connection if you are using multiple databases for your tenants. Commented Jul 23, 2020 at 14:16
  • There is a good chance that assertDatabaseHas() is working, and the test is successfully telling you that the code is not working as you intend it to. What is happening in the controller for hub.customer.updateStatus? Commented Jul 23, 2020 at 14:36

1 Answer 1

0

I would you recommend to change assertDatabaseHas to assertEquals.

Here is why:

"When you’re working with Eloquent, you specify a table name - or it automatically figures it out. Then, you forget about it. So, it’s ideally designed for you not to have to know the name of the table."

"Laravel is architected, then, so that we don’t have to know the names of our database tables. With assertDatabaseHas you have to know the name of the table every time you use it."

"But, I think it’s best to stop asserting directly against a database when you don’t need to. Especially since your code is not generally architected in Laravel to deal directly with the database, why would your tests? Stay in your domain and test the input and output values, not the implementation."

From a article https://www.aaronsaray.com/2020/stop-assert-database-has-laravel

Sign up to request clarification or add additional context in comments.

4 Comments

Can you quote relevant excerpts from that article? Links tend to die over time
For this reason, I always use the following (new MyModel())->getTable()
@Kwaadpepper You could just pass the class $this->assertDatabaseHas(Person::class, $person)
also you can just pass the model class, $this->assertDatabaseHas(Post::class, [])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.