我有一个单元验收测试,我正在嘲笑用户的创建。
class UserAcceptanceApiTest extends TestCase
{
use WithoutMiddleware;
public function setUp()
{
parent::setUp();
$this->User = factory(AppModelsUser::class)->make([
'id' => '999',
'name' => 'Name',
'email' => 'test@example.com',
'password' => bcrypt('password'),
]);
$this->User = factory(AppModelsUser::class)->make([
'id' => '999',
'name' => 'Name',
'email' => 'test@example.com',
'password' => bcrypt('password'),
]);
$user = factory(AppModelsUser::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]);
}
}
我得到以下异常"Field 'password' doesn't have a default value
。
这是因为在我的User
模型中,我有以下内容:
protected $hidden = ['password', 'remember_token'];
因此,它会自动从 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]);
}