Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deladels's avatar

Model Refresh Not Working with Tests

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);
    }
0 likes
11 replies
MichalOravec's avatar

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);
Tray2's avatar

You are asserting that the updated value is $this->assertEquals('Accelerator Program New', $program->description); while you set it to description' => 'Accelerator 2020 Program',

bobbybouwmann's avatar

Your code looks correct to me. The refresh should go fine here as well.

Did you add the name, description, and cost field to the $fillable array in your Program model? Laravel ignores the fields if they are not whitelisted.

deladels's avatar

@bobbybouwmann Yes they are there in my model.

class Program extends Model
{
    protected $fillable = [
        'name',
        'description',
        'cost'
    ];

}
deladels's avatar

@michaloravec it was a typo. Changed, it still fails.

Time: 12.61 seconds, Memory: 24.00 MB

There was 1 failure:

1) Tests\Feature\ProgramsTest::a_program_can_be_updated
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Accelerator 2020'
+'Accelerator'
deladels's avatar

@tray2 updated that. Still fails.

Time: 12.61 seconds, Memory: 24.00 MB

There was 1 failure:

1) Tests\Feature\ProgramsTest::a_program_can_be_updated
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Accelerator 2020'
+'Accelerator'
MichalOravec's avatar
Level 75

@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());
1 like
bobbybouwmann's avatar

@deladels So I think the problem here is that you might get an incorrect response from the test.

Try this to make sure your request is going correct

$response = $this->patch(route('programs.update', $program), [
    'name' => 'Accelerator 2020',
    'description' => 'Accelerator 2020 Program',
    'cost' => 200
]);

$response->assertOk();

If this fails, it either means you have an error or something is wrong with the validation.

1 like
nmeri17's avatar

Yea, not directly related to your question but I faced same challenge. It eventually turned out to be caused by my enum column. In the migration, the values were filled with integers coming from class constants on the model. I tried to update that column using same constants and the method was returning true despite silently failing

So I wrapped the consts in strval and the update operation was being performed successfully

Please or to participate in this conversation.