Testing form with relationships

Hi, I'm attempting to test a create form for my Users page. The form has basic info on, as well as a roles dropdown which uses filament-shield. When using the UI, everything works fine. When I run the test to make sure there are no errors, the test fails with the following and I can't work out why:
FAILED Tests\Feature\UserTest > `Create Page` can save form
Component has errors: "data.roles"
Failed asserting that false is true.

at vendor/livewire/livewire/src/Features/SupportValidation/TestsValidation.php:109
105▕ {
106▕ $errors = $this->errors();
107▕
108▕ if (empty($keys)) {
109▕ PHPUnit::assertTrue($errors->isEmpty(), 'Component has errors: "'.implode('", "', $errors->keys()).'"');
110▕
111▕ return $this;
112▕ }
113▕
FAILED Tests\Feature\UserTest > `Create Page` can save form
Component has errors: "data.roles"
Failed asserting that false is true.

at vendor/livewire/livewire/src/Features/SupportValidation/TestsValidation.php:109
105▕ {
106▕ $errors = $this->errors();
107▕
108▕ if (empty($keys)) {
109▕ PHPUnit::assertTrue($errors->isEmpty(), 'Component has errors: "'.implode('", "', $errors->keys()).'"');
110▕
111▕ return $this;
112▕ }
113▕
Test
test('can save form', function () {
$user = User::factory()->make();

livewire(CreateUser::class)
->fillForm($user->toArray())
->call('create')
->assertHasNoFormErrors();
});
test('can save form', function () {
$user = User::factory()->make();

livewire(CreateUser::class)
->fillForm($user->toArray())
->call('create')
->assertHasNoFormErrors();
});
Factory
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'status' => $this->faker->randomElement(UserStatus::class),
];
}
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'status' => $this->faker->randomElement(UserStatus::class),
];
}
Roles dropdown in form schema
Select::make('roles')
->relationship('roles', 'name')
->getOptionLabelFromRecordUsing(function (Model $record) {
/** @var Role $record */
return UserRole::getRoleLabel($record->name);
})
->preload()
->searchable()
->required()
->dehydrated(false),
Select::make('roles')
->relationship('roles', 'name')
->getOptionLabelFromRecordUsing(function (Model $record) {
/** @var Role $record */
return UserRole::getRoleLabel($record->name);
})
->preload()
->searchable()
->required()
->dehydrated(false),
TIA, Adam
1 Reply
Adam Holmes
Adam HolmesOP8mo ago
I added a role, to the factory, but that still resulted in the same errors

Did you find this page helpful?