我有一个使用DB的包,我想为这些测试创建一些在内存中使用sqlite运行的测试。
现在我有了这个基础测试类:
use Illuminate\Database\Capsule\Manager;
class TestCaseDb extends \PHPUnit_Framework_TestCase {
protected $db;
protected $tbmsg;
public function setUp()
{
parent::setUp(); // //DONT CARE
League\FactoryMuffin\Facade::getFaker()->unique($reset = true);//DONT CARE
$this->initDb();
$this->initTbmsg(); //DONT CARE
}
protected function initDb() {
//confi gfor the sqlite
$capsule = new Manager();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$this->db = $capsule->getDatabaseManager();
//loading simple DB tables creation
$importSql = file_get_contents(__DIR__.'/dumps/dump.sql');
$this->db->statement($importSql);
}
}现在,您可以在这里看到,我创建了sqlite数据库,并创建了用于处理它的eloquent DB对象。
但是,现在如果我用
$this->db->select("whatever");它工作得很好。
但是当我尝试使用一个有说服力的对象时,它会告诉我特定的表不存在。(它100%存在于第一个Db中)
因此,我认为eloquent模型尝试连接到另一个数据库连接,而不是我创建的那个。
IE -这是给出错误的测试:
class SimpleTest extends TestCaseDb {
/**
* @test
*/
public function first() {
//the below row works!
//$this->db->insert('insert into conv_users (conv_id, user_id) values (?, ?)', array(1, 2));
//the insert with Eloquent fails....
$data = League\FactoryMuffin\Facade::create('Tzookb\TBMsg\Models\Eloquent\Conversation', []);
$this->assertTrue(true);
}
}您也可以在我的github包中看到代码:(branch dev) https://github.com/tzookb/tbmsg/tree/dev/tests
发布于 2015-01-06 04:14:45
你正在做的事情是不必要的。
Laravel为您提供了开箱即用的工具来完成您正在尝试做的事情。
将此代码添加到TestCase的setUp()中。
Artisan::call('migrate');
$this->seed();它将迁移您在SQLite memory数据库中的迁移。
那么你应该有这样的东西:
<?php
class TestCase extends \Illuminate\Foundation\Testing\TestCase {
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
$this->seed();
}
/**
* Creates the application.
*
* @return \Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
}https://stackoverflow.com/questions/27786967
复制相似问题