vendredi 25 janvier 2019

Laravel Factory relationship @ Testing

Okay so, I'm developing test cases (I like the idea, no idea if it's how it's supposed to be approached),as in 1 per role (My application contains roles and 1 user can have 1 role)

abstract class AdminTestCase extends BaseTestCase
{
    use CreatesApplication;

    protected $headers;
    protected $user;

    public function setUp()
    {
        parent::setUp();
        $fakeUser = factory(User::class)->make(['id' => 1,
             'role_id' => 1,
             'name' => 'Maintenence test case',
             'email' => 'admin@admin.com',
             'username' => 'MaintenenceTestCase',
             'password' => Hash::make('test'),
             'enabled' => 1])->setRelation('role', factory(Role::class)->make(['id' => '1', 'name' => 'Maintenence role']));
        $this->user = $fakeUser;

        //This is what I want to test further down the line
        $this->user->create();

        //Ignore what's below here
        $this->headers['Accept'] = 'Application/json';
    }
}


The idea is that I want some tests to be able to consider an empty database (mocking some results) and some tests that contains records. On these second tests, I want be able to call $this->user->create() and insert the User and Role into the database, but this is currently not working as it claims role_id can't be null and I kinda want to create this dependency (When the creating the user, creates the role) $this->user

Aucun commentaire:

Enregistrer un commentaire