Nab*_*sta 6 database phpunit unit-testing seeding laravel-5
我从一个种子数据库开始,我试图在Laravel 5中的单元测试之间重新设置数据库.在Laravel 4中我理解你可以简单地使用Illuminate\Support\Facades\Artisan并运行命令
Artisan::call('migrate');
Artisan::call('db:seed');
Run Code Online (Sandbox Code Playgroud)
或者你认为可以这样做:
$this->seed('DatabaseSeeder');
Run Code Online (Sandbox Code Playgroud)
在每次测试之前.在Laravel 5中,这似乎已被替换为
use DatabaseMigrations;
Run Code Online (Sandbox Code Playgroud)
要么
use DatabaseTransactions;
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用这些并且设法获得测试以迁移数据库; 但是,它实际上并没有重新设置表格中的数据.我已经阅读了几个论坛抱怨这个,并尝试了几种不同的方法从TestCase和每个测试中调用这些...添加
$this->beforeApplicationDestroyed(function () {
Artisan::call('migrate');
Artisan::call('migrate:reset');
Artisan::call('db:seed');
DB::disconnect();
});
Run Code Online (Sandbox Code Playgroud)
到TestCase.php tearDown()
......
我也尝试过添加
$this->createApplication();
Run Code Online (Sandbox Code Playgroud)
从TestCase.php的每个测试中调用的方法
有时它只是完全擦掉我的桌子.我在Laravel的网站或博客中找到的任何内容似乎都无效.部分原因可能是因为我可能在Laravel 5中尝试Laravel 4方法.在Laravel 5中有没有办法做到这一点?
我的testcase.php代码如下:
<?php
use Illuminate\Support\Facades\Artisan as Artisan;
class TestCase extends Illuminate\Foundation\Testing\TestCase{
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
protected $baseUrl = 'http://localhost';
public function initializeTests(){
$this->createApplication();
Artisan::call('migrate');
$this->artisan('migrate');
Artisan::call('db:seed');
$this->artisan('db:seed');
$this->seed('DatabaseSeeder');
$this->session(['test' => 'session']);
$this->seed('DatabaseSeeder');
}
public function tearDown()
{
Mockery::close();
Artisan::call('migrate:reset');
$this->artisan('migrate:reset');
Artisan::call('migrate:rollback');
$this->artisan('migrate:rollback');
Artisan::call('migrate');
$this->artisan('migrate');
Artisan::call('db:seed');
$this->artisan('db:seed');
$this->seed('DatabaseSeeder');
DB::disconnect();
foreach (\DB::getConnections() as $connection) {
$connection->disconnect();
}
$this->beforeApplicationDestroyed(function () {
Artisan::call('migrate:reset');
$this->artisan('migrate:reset');
Artisan::call('migrate:rollback');
$this->artisan('migrate:rollback');
Artisan::call('migrate');
$this->artisan('migrate');
Artisan::call('db:seed');
$this->artisan('db:seed');
$this->seed('DatabaseSeeder');
DB::disconnect();
foreach (\DB::getConnections() as $connection) {
$connection->disconnect();
}
});
$this->flushSession();
parent::tearDown();
}
public function getConnection()
{
$Connection = mysqli_connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USERNAME'], $GLOBALS['DB_PASSWORD'], $GLOBALS['DB_DATABASE']);
$this->createDefaultDBConnection();
return $this->Connection;
}
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
/**
* Magic helper method to make running requests simpler.
*
* @param $method
* @param $args
* @return \Illuminate\Http\Response
*/
public function __call($method, $args)
{
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete']))
{
return $this->call($method, $args[0]);
}
throw new BadMethodCallException;
}
/**
* Create a mock of a class as well as an instance.
*
* @param $class
* @return \Mockery\MockInterface
*/
public function mock($class)
{
$mock = Mockery::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试看起来像
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Artisan;
class CustomerRegistrationControllerTest extends TestCase
{
use DatabaseMigrations;
protected static $db_inited = false;
protected static function initDB()
{
echo "\n---Customer Registration Controller Tests---\n"; // proof it only runs once per test TestCase class
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function setUp()
{
parent::setUp();
if (!static::$db_inited) {
static::$db_inited = true;
static::initDB();
}
// $this->app->refreshApplication();
$this->artisan('migrate:refresh');
$this->seed();
$this->seed('DatabaseSeeder');
$this->initializeTests();
);
}
public function testSomething()
{
$this->Mock
->shouldReceive('destroy')
->with('1')
->andReturn();
$this->RegistrationController->postRegistration();
// $this->assertResponseStatus(200);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3077 次 |
最近记录: |