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?