Filament form with laravel pest.

This test fail sometimes. Where am I do wrong?
it(
'can ensure there are no validation errors',
fn() => [
actingAs(\App\Models\User::factory()->create()),

($status = \App\Models\Status::factory()->create([
'name' => 'Status is opening',
])),

($category = \App\Models\Category::factory()->create([
'name' => 'This is category one',
])),

livewire(CreateFeeling::class)
->fillForm([
'title' => fake()->sentence(),
'description' => fake()->sentence(),
'category_id' => $category->id,
'status_id' => $status->id,
])
->call('create')
->assertHasNoFormErrors([
'title' => 'required',
'description' => 'required',
]),
],
);
it(
'can ensure there are no validation errors',
fn() => [
actingAs(\App\Models\User::factory()->create()),

($status = \App\Models\Status::factory()->create([
'name' => 'Status is opening',
])),

($category = \App\Models\Category::factory()->create([
'name' => 'This is category one',
])),

livewire(CreateFeeling::class)
->fillForm([
'title' => fake()->sentence(),
'description' => fake()->sentence(),
'category_id' => $category->id,
'status_id' => $status->id,
])
->call('create')
->assertHasNoFormErrors([
'title' => 'required',
'description' => 'required',
]),
],
);
12 Replies
Dan Harrin
Dan Harrin2y ago
i understand the question but to expect us to know what the problem is without even an error message or debugging info when the test fails... how can we help you?
Shaung Bhone
Shaung BhoneOP2y ago
It was not passed the first time I ran ./vendor/bin/sail pest. The second time it passed.🥹
krekas
krekas2y ago
something with your factory and/or migrations
Shaung Bhone
Shaung BhoneOP2y ago
public function up(): void
{
Schema::create('feelings', function (Blueprint $table) {
$table->id();
$table
->foreignId('user_id')
->constrained()
->cascadeOnDelete();
$table
->foreignId('status_id')
->constrained()
->cascadeOnDelete();
$table
->foreignId('category_id')
->constrained()
->cascadeOnDelete();
$table->string('title');
$table->string('slug')->nullable();
$table->text('description');
$table->timestamps();
});
}
public function up(): void
{
Schema::create('feelings', function (Blueprint $table) {
$table->id();
$table
->foreignId('user_id')
->constrained()
->cascadeOnDelete();
$table
->foreignId('status_id')
->constrained()
->cascadeOnDelete();
$table
->foreignId('category_id')
->constrained()
->cascadeOnDelete();
$table->string('title');
$table->string('slug')->nullable();
$table->text('description');
$table->timestamps();
});
}
public function create()
{
if (auth()->check()) {
$data = $this->form->getState();

$record = Feeling::create(
array_merge($data, [
'user_id' => auth()->id(), 'status_id' => 1
])
);

//

return redirect()->route('forum.index');
}

abort(Response::HTTP_FORBIDDEN);
}
public function create()
{
if (auth()->check()) {
$data = $this->form->getState();

$record = Feeling::create(
array_merge($data, [
'user_id' => auth()->id(), 'status_id' => 1
])
);

//

return redirect()->route('forum.index');
}

abort(Response::HTTP_FORBIDDEN);
}
Dennis Koch
Dennis Koch2y ago
For your test you create a new status, but in the controller you always assume there is a status with ID 1
Shaung Bhone
Shaung BhoneOP2y ago
Thank you. One more question: how can I make this? What's the best way to do this?
Dennis Koch
Dennis Koch2y ago
Make what? Why do you use a fixed ID in your controller but not in your test?
Shaung Bhone
Shaung BhoneOP2y ago
$table
->foreignId('status_id')
->default(1)
->constrained()
->cascadeOnDelete();
$table
->foreignId('status_id')
->default(1)
->constrained()
->cascadeOnDelete();
->fillForm([
'title' => fake()->sentence(),
'description' => fake()->sentence(),
'category_id' => $category->id,
'status_id' => 1,
])
->fillForm([
'title' => fake()->sentence(),
'description' => fake()->sentence(),
'category_id' => $category->id,
'status_id' => 1,
])
it's not working.
Dennis Koch
Dennis Koch2y ago
Why are you doing this 'status_id' => 1,?
Shaung Bhone
Shaung BhoneOP2y ago
here.
Dennis Koch
Dennis Koch2y ago
But why are you creating a new Status in your test?
($status = \App\Models\Status::factory()->create([
'name' => 'Status is opening',
])),
($status = \App\Models\Status::factory()->create([
'name' => 'Status is opening',
])),
Shaung Bhone
Shaung BhoneOP2y ago
I'm not sure if this is the best way or not. now I removed it. I got it now thank you.
Want results from more Discord servers?
Add your server