It fails because you compare wrong data. It should be.
$this->assertEquals('Accelerator 2020', $program->name);
$this->assertEquals('Accelerator 2020 Program', $program->description);
$this->assertEquals(200, $program->cost);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am running a test to assert if a resource can be updated. However, the test keeps failing and I can't seem to figure out why.
See below my code.
ProgramTest.php
public function a_program_can_be_updated()
{
$this->withoutExceptionHandling();
$program = factory(Program::class)->create(
[
'name' => 'Accelerator',
'description' => 'Accelerator Program',
'cost' => 150
]
);
$this->patch(route('programs.update', $program), [
'name' => 'Accelerator 2020',
'description' => 'Accelerator 2020 Program',
'cost' => 200
]);
$program->refresh();
$this->assertEquals( 'Accelerator 2020', $program->name);
$this->assertEquals('Accelerator 2020 Program', $program->description);
$this->assertEquals(200 , $program->cost);
}
ProgramController.php
public function update(Request $request, Program $program)
{
$program->update([
$request->only(['name','description','cost'])
]);
return redirect()->route('programs.show', $program);
}
@deladels You have there an array in update, so change it to this.
$program->update($request->only([
'name','description','cost'
]));
Or more simple with this, because you have set fillable on the model
$program->update($request->all());
Please or to participate in this conversation.