use*_*492 2 php passwords laravel laravel-5
我有一个单元验收测试,我在模拟用户的创建。
class UserAcceptanceApiTest extends TestCase
{
use WithoutMiddleware;
public function setUp()
{
parent::setUp();
$this->User = factory(App\Models\User::class)->make([
'id' => '999',
'name' => 'Name',
'email' => 'test@example.com',
'password' => bcrypt('password'),
]);
$this->User = factory(App\Models\User::class)->make([
'id' => '999',
'name' => 'Name',
'email' => 'test@example.com',
'password' => bcrypt('password'),
]);
$user = factory(App\Models\User::class)->make();
$this->actor = $this->actingAs($user);
}
public function testStore()
{
$response = $this->actor->call('POST', 'api/users', $this->User->toArray());
$this->assertEquals(200, $response->getStatusCode());
$this->seeJson(['id' => 999]);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下异常"Field 'password' doesn't have a default value。
这是因为在我的User模型中,我有以下内容:
protected $hidden = ['password', 'remember_token'];
Run Code Online (Sandbox Code Playgroud)
因此它会自动从 JSON 中删除密码字段。
有没有办法只针对这个测试覆盖它?因为我想将密码保留为隐藏属性。
public function testStore()
{
$this->User->makeVisible(['password']);
$response = $this->actor->call('POST', 'api/users', $this->User->toArray());
$this->assertEquals(200, $response->getStatusCode());
$this->seeJson(['id' => 999]);
}
Run Code Online (Sandbox Code Playgroud)