如果在Laravel 5.5单元测试中隐藏密码,如何模拟用户创建

时间:2018-02-28 16:10:05

标签: php laravel laravel-5 passwords

我有一个单元验收测试,我在嘲笑用户的创建。

["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中删除密码字段。

有没有办法可以仅为此测试覆盖此内容?因为我想将密码保留为隐藏属性。

1 个答案:

答案 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]);
}