2014-02-21 108 views
4

我运行Laravel,在OS X上使用MAMP,我试图做一些单元测试,使用PHPUnit和sqlite,但是当我运行我的测试时,出现错误Laravel测试,没有这样的表,sqlite

General error: 1 no such table: users

我试过运行工匠,手动迁移数据库,使用--env = testing,它运行良好,但我仍然得到错误。我甚至在SetUp方法中调用Artisan::call('migrate');

/app/config/testing/database.php

return [ 
    'default' => 'sqlite', 

    'connections' => [ 
     'sqlite' => [ 
      'driver' => 'sqlite', 
      'database' => ':memory:', 
      'prefix' => '' 
     ], 
    ] 
]; 

编辑: 迁移文件:

Schema::create('users', function($table) { 
    $table->increments('id'); 
    $table->string('email', 32)->unique(); 
    $table->string('password', 64); 
    $table->string('username', 32)->unique(); 
    $table->dateTime('birthdate'); 
    $table->enum('status', array('ACTIVE', 'INACTIVE')); 
    $table->timestamps(); 
}); 

Schema::create('roles', function($table) { 
    $table->increments('id'); 
    $table->string('name', 32); 
}); 

Schema::create('role_user', function ($table) { 
    $table->increments('id'); 

    $table->unsignedInteger('user_id'); 
    $table->unsignedInteger('role_id'); 

    $table->foreign('user_id')->references('id')->on('users'); 
    $table->foreign('role_id')->references('id')->on('roles'); 

}); 
+0

你能告诉我们你的迁移创建表用户吗?你在其他应用中使用哪个数据库? MySQL? –

+0

是的,它是mysql,当我恢复工作时,我将添加迁移文件。 – Kao

+0

在编辑中添加了迁移 – Kao

回答

6

SQLite不支持ENUM类型:http://www.sqlite.org/datatype3.html

这就是为什么没有创建用户表。您将不得不在经典的MySQL表上运行测试或删除所有ENUM类型。

+0

解决了我的问题,改为使用“Enum”表。谢谢。 – Kao

1

运行测试之前运行的迁移不起作用,因为,作为你的config显示,你的db在内存,所以每次你运行一个新的测试时,它会为每个测试会话运行迁移,并在完成时彻底销毁所有测试数据。

要使用内存数据库,运行迁移每次运行测试时,从测试用例的东西,如延长你的类:

<?php 

class TestCase extends Illuminate\Foundation\Testing\TestCase { 

    public function createApplication() 
    { 
     $unitTesting = true; 
     $testEnvironment = 'testing'; 
     return require __DIR__.'/../../bootstrap/start.php'; 
    } 

    public function setUp() 
    { 
     parent::setUp(); 
     $this->prepareForTests(); 
    } 
    private function prepareForTests() 
    { 
     Artisan::call('migrate'); 
     Artisan::call('db:seed'); 
    } 
    public function tearDown() 
    { 
     parent::tearDown(); 
    } 
} 
+0

这大概是我的测试看起来的样子。 – Kao