You need to be a bit more specific and preferably post some code snippet to illustrate your problem.
PHPunit test sometimes fails....
I have no idea why the result of the test can be changed.....
Is there anyone who knows that problem and some assumable reasons???
My phpunit test sometimes fails at this point(completing_a_task)
$project = ProjectFactory::withTasks(1)->create();
$this->actingAs($project->owner)->patch($project->tasks->first()->path(), [
'body' => 'foobar',
'completed' => true
]);
$this->assertCount(3, $project->activity);
tap($project->activity->last(), function ($activity) {
dd($activity->description);
$this->assertEquals('completed_task', $activity->description);
$this->assertInstanceOf(Task::class, $activity->subject);
});
I know that we assume that we are gonna get 'completed_task' from $project->activity->last() in tap function
However, I do get sometimes 'created_task' instead of 'completed_task' that we expect. So the part of test fails
I'm also using RfreshDatabase trait above.
Is there anyone who knows that issue and assumable problem?? @Jeff
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
I found that when I fetch like this $project->activity, It has rarely different sequence.
for that reason, $project->activity->last() gets different attributes.
That's why the assertion below fails sometimes.
$this->assertEquals('completed_task', $activity->description);
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
I figured it out.
The problem was time. In Project Model, we have that relationship and fetch by using latest()
public function activity()
{
return $this->hasMany(Activity::class)->latest();
}
when I did the test above, I get activities(3) at the exact same time most but rarely there was one second gap between second activity and third activity,So the third one was stored in array as a first. furthermore, since we are retrieving them by chaining last() which means getting the last one in array. That' the reason why I got 'created_task' not 'completed_task'
I hope that my writing can help someone like me. :)
Thank you for your reply :)
Please or to participate in this conversation.