mardi 27 octobre 2015

Use existing migrations tables for laravel unit testing

I want to use an existing set of migrations to create tables for me to stuff seed data into. Examples like this show creating the table on the fly:

DB::schema()->create('oauth_identities', function($table) {...

But I want to use existing migrations.

public function setUp()
{
    parent::setUp();

    $this->artisan = $this->app->make('Illuminate\Contracts\Console\Kernel');
    $this->artisan->call(
        'migrate', [
            '--database' => 'testbench',
            '--path'     => '../database/migrations',
        ]
    );


}

protected function getEnvironmentSetUp($app)
{
    parent::getEnvironmentSetUp($app);

    $app['config']->set('database.default', 'testbench');
    $app['config']->set(
        'database.connections.testbench', [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => ''
        ]
    );
}

UserTest.php:

private function seedUser()
{

    return [
        'email' => 'myemail',
         ...
    ];
    ...
}

...
public function testCreateUser()
{
    $user = User::create($this->seedUser());

    die(print_r($user)); 
    ...

Error:

In \App\Tests\UserTest::testCreateUser General error: 1 no such table: users

But in my migrations, I am definitely creating the table:

Schema::create('users', function (Blueprint $table) {...

So how can I use existing migration tables and generate seed data for the migration?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire