Summer Sale! All accounts are 50% off this week.

BernardoBF4's avatar

Organising my tests

I currently have a test which tests if a request will return the correct message. In this case, the data is the expected one and the message is a success one.

/** @test */
  public function a_logged_user_can_create_a_group()
  {
    $this->withoutExceptionHandling()->signIn();

    $group_data = [
      'name' => $this->faker->word(),
      'status' => $this->faker->boolean(),
    ];
    $modules = ['modules' => Modules::factory(1)->create()->pluck('id')];

    $response = $this->post(route('cms.groups.store'), array_merge($group_data, $modules));

    $response->assertSessionHas('message', trans('cms.groups.success_create'));
  }

But I also feel like I need to check wether the data I sent through my route is actually being persisted to the database or not, thus I created a new test for that. But I don't know if the test bellow is supposed to be a feature test or a unit test, because I don't know if feature tests should deal with the database directly.

/** @test */
  public function when_a_logged_user_creates_a_group_the_data_is_persisted_to_the_database()
  {
    $this->withoutExceptionHandling()->signIn();

    $group_data = [
      'name' => $this->faker->word(),
      'status' => $this->faker->boolean(),
    ];
    $modules = ['modules' => Modules::factory(1)->create()->pluck('id')];

    $this->post(route('cms.groups.store'), array_merge($group_data, $modules));

    $this->assertDatabaseHas('groups', $group_data);
  }

Also, I have another doubt about my second test: I'd also like to assert that the pivot table has the relation between the group and module persisted, but I don't the group id; so should I perform a query to find the register with the given data and get it's id or is there a better way?

0 likes
5 replies
Tray2's avatar

This is an example on how I done it for one of my apps, it is written in PEST but the theory is the same. The valid book is an array that I create in the setup.

it('stores a valid book', function () {
    post(route('books.store', $this->validBook))
    ->assertRedirect(route('books.index'));
    assertDatabaseCount('books', 1); //Checking that there is a book in the database
    assertDatabaseCount('author_book', 1); //Checking that there is a row in the pivot table
});

You can see the complete test file here

https://github.com/Tray2/mediabase/blob/main/tests/Feature/Books/BookStoreTest.php

1 like
BernardoBF4's avatar

@Tray2 Oh, if I understood it correctly you didn't validate if the data itself was present in the database, but if the database had on register on each tabçe you wanted? That's a nice ideia, but if I have many tests running at once, can't another test save some data and then accidently validate this test?

Also, any ideia on wether my test should a feature or unit test?

Sinnbeck's avatar

@BernardoBF4 tests run one by one and each test should rollback the database to a clean state (RefreshDatabase)

1 like
BernardoBF4's avatar

@Sinnbeck I thought the database was cleaned after every test had ran. Thanks, that changes my mindset for testing now :)

Please or to participate in this conversation.