Tôi bắt đầu với một cơ sở dữ liệu hạt giống và đang cố gắng chèn sẵn cơ sở dữ liệu giữa các thử nghiệm đơn vị trong Laravel 5. Trong Laravel 4, tôi hiểu rằng bạn có thể sử dụng Illuminate \ Support \ Facades \ Artisan và chạy các lệnhLaravel 5 Nhập lại cơ sở dữ liệu để kiểm tra đơn vị giữa các thử nghiệm
Nghệ nhân :: gọi ('di chuyển'); Artisan :: call ('db: seed');
hoặc bạn được cho là có thể làm:
$ this-> seed ('DatabaseSeeder');
trước mỗi lần kiểm tra. Trong Laravel 5, điều này dường như đã được thay thế bằng
sử dụng DatabaseMigrations; hoặc sử dụng DatabaseTransactions;
Tôi đã thử sử dụng các tính năng này và đã quản lý để có được các thử nghiệm để di chuyển cơ sở dữ liệu; tuy nhiên, nó không thực sự chèn sẵn dữ liệu trong các bảng. Tôi đã đọc qua một số diễn đàn phàn nàn về vấn đề này và đã thử các cách tiếp cận khác nhau gọi những từ TestCase và bên trong mỗi thử nghiệm ... thêm
$this->beforeApplicationDestroyed(function() {
Artisan::call('migrate');
Artisan::call('migrate:reset');
Artisan::call('db:seed');
DB::disconnect();
});
đến TestCase.php teardown() ...
tôi cũng đã thử thêm
$this->createApplication();
đến một phương pháp gọi trong mọi thử nghiệm từ TestCase.php
Đôi khi nó chỉ lau bảng của tôi ra hoàn chỉnh ly. Không có gì tôi tìm thấy trên trang web của Laravel hoặc trong các blog dường như hoạt động. Một phần của nó có lẽ là vì tôi có thể đang thử các phương thức Laravel 4 trong Laravel 5. Có cách nào để làm điều này trong Laravel 5 không?
Mã của tôi cho testcase.php trông giống như:
<?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;
}
}
thử nghiệm của tôi trông giống như
<?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);
}
}
Có thể liên kết này sẽ giúp https://laracasts.com/discuss/channels/tips/how-to-set-up-and-define-your-database-for-integration-testing – Raftalks