mercredi 28 février 2018

How to mock user creation if password is hidden in Laravel 5.5 unit tests

I have a unit acceptance test where I am mocking the creation of a user.

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

}

I get the following exception "Field 'password' doesn't have a default value.

This is because in my User model I have the following:

protected $hidden = ['password', 'remember_token'];

So it automatically removes the password field from the JSON.

Is there a way I can override this only for this test? As I want to keep the password as a hidden attribute.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire