Ad

PHPUnit Laravel Couldn't Update In Ut

- 1 answer

I have been meeting an annoying problem when using phpunit. I found my database is not updated, after an eloquent method is triggered, so the result of the unit test is always wrong.

The code is as below:

$testTaskUser = TestTaskUser::find(\SeederConstants::$TEST_TASK_USER_ID);
$testTaskUser->update(array(
            'status' => TestTaskUserStatus::$INITIALIZED
    ));
$this->assertEquals($testTaskUser->status,TestTaskUserStatus::$INITIALIZED);
//This assert fails because the 'status' is not updated in the db, which is 
//Very confusing to me

The phpserver and mysql is running in vm in Ubuntu, on my Macbook air. Anybody can help will be really appreciated.

Ad

Answer

You should make sure that for TestTaskUser user you've added status column to $fillable.

protected $fillable = ['status'];

Please look at mass assignment section

Alternatively you can change your code from:

$testTaskUser->update(array(
            'status' => TestTaskUserStatus::$INITIALIZED
    ));

to:

$testTaskUser->status = TestTaskUserStatus::$INITIALIZED;
$testTaskUser->save();

and in this case you don't need to fill in fillable property

Ad
source: stackoverflow.com
Ad