->assertSessionHasErrors(['description']);
https://laravel.com/docs/9.x/http-tests#assert-session-has-errors
I'm creating a feature test for one of my models with the following code:
CollectionTest.php
/** @test*/
public function a_collection_requires_a_description()
{
$user = User::factory()->create();
$attributes = Collection::factory()->raw(['description' => '']);
$this->actingAs($user)
->post('/collections', $attributes)
->assertSessionHasErrors('description'); //This is where the error is thrown
}
CollectionFactory.php
public function definition()
{
return [
'name' => fake()->firstName() . ' ' . fake()->lastName(),
'description' => fake()->paragraph(),
'filename' => 'https://picsum.photos/200/300',
'release_date' => fake()->dateTimeBetween('-1 year', '+1 year'),
'published_at' => fake()->dateTime()
];
}
CollectionController.php
public function store()
{
request()->validate([
'name' => 'required',
'description' => 'required',
'filename' => 'nullable',
'release_date' => 'nullable',
'published_at' => 'nullable',
]);
Collection::create(request(['name', 'description']));
return redirect('/collections');
}
When I run the test, I receive the following error:
Expected a scalar, or an array as a 2nd argument to "Symfony\Component\HttpFoundation\InputBag::set()", "DateTime" given.
The error is thrown at the assertSessionHasErrors function in the CollectionTest.php file. If I comment out everything except the name and description keys in the CollectionFactory.php file it works. Any thoughts on what could be causing this?
Please or to participate in this conversation.