gpt4 book ai didi

database - Laravel 5 为测试之间的单元测试重新播种数据库

转载 作者:太空狗 更新时间:2023-10-30 01:49:48 25 4
gpt4 key购买 nike

我从一个播种数据库开始,并尝试在 Laravel 5 中的单元测试之间重新播种数据库。在 Laravel 4 中,我知道你可以简单地使用 Illuminate\Support\Facades\Artisan 并运行命令

Artisan::call('migrate');
Artisan::call('db:seed');

或者你应该可以这样做:

$this->seed('DatabaseSeeder');

在每次测试之前。在 Laravel 5 中,这似乎已被替换为

use DatabaseMigrations;

use DatabaseTransactions;

我已经尝试使用这些并成功地获得了迁移数据库的测试;但是,它实际上并没有重新播种表中的数据。我已经阅读了几个提示这个的论坛,并尝试了几种不同的方法从 TestCase 和每个测试中调用这些...添加

    $this->beforeApplicationDestroyed(function () {
Artisan::call('migrate');
Artisan::call('migrate:reset');
Artisan::call('db:seed');
DB::disconnect();
});

到 TestCase.php tearDown()...

我也试过添加

$this->createApplication();

从 TestCase.php 到每个测试中调用的方法

有时它会把我的 table 彻底擦干净。我在 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;
}

}

我的测试看起来像

<?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);

}

}

最佳答案

只需运行这个:

    $this->artisan('migrate:refresh', [
'--seed' => '1'
]);

为避免数据库更改在测试之间持续存在,请将 use DatabaseTransactions 添加到命中数据库的测试中。

关于database - Laravel 5 为测试之间的单元测试重新播种数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com