我的测试创建了一个包含两个Account
的{{1}},其中一个是“管理员” User
,他可以向该帐户上的其他Role
授予特权帐户,另一个是常规User
,未分配User
。
Role
// AccountUsersTest.php
public function superusers_can_promote_users()
{
$account = AccountFactory::withUsers(1)->create();
$superuser = $account->superUser;
$user = $account->regUsers->first();
$this->actingAs($superuser)
->post($user->path(), ['role' => 'manager']);
$this->assertTrue($user->hasRole('manager')); // <-- passes assertion
$this->actingAs($superuser)
->post($user->path(), ['role' => 'user']);
$this->assertTrue($user->hasRole('user')); // <-- fails assertion
}
// routes/web.php
Route::post('/account/users/{user}', 'AccountUsersController@update')->name('account.users.update');
// AccountUsersController.php
public function update(Request $request, User $user)
{
$this->authorize('manageAccountUsers', $user->account);
if ($role = request('role'))
{
$user->updateRole($role);
}
return redirect($user->path());
}
// User.php
public function updateRole($role)
{
$this->syncRoles($role);
var_dump($this->hasRole('user')); // <-- during the test, returns FALSE then TRUE (as expected)
}
s已正确地植入数据库中。Role
是以“ Birdboard” Laracasts TDD系列中使用的class-based model
factory为模型的,
通过许多其他测试,我认为这不是问题。 AccountFactory
返回包含指向端点的URL的字符串。$user->path()
方法时,授权都会通过
叫。第二个断言为什么失败?