我有一个单元验收测试,我在嘲笑用户的创建。
["59SDS64675A00096D48CB","59SDS64675A00096D59DB"]
我收到以下异常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]);
}
}
。
这是因为在我的"Field 'password' doesn't have a default value
模型中,我有以下内容:
User
因此它会自动从JSON中删除密码字段。
有没有办法可以仅为此测试覆盖此内容?因为我想将密码保留为隐藏属性。
答案 0 :(得分:2)
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]);
}