我正在尝试让 Laravel 项目在我的新工作中运行。离开的开发者在移交文档中没有具体说明如何让代码运行起来,其他人也不知道。
SQLite 数据库是使用 :memory:
创建的,php artisan migrate
可以正常工作。
...
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated: 2016_06_01_000004_create_oauth_clients_table
...
特征(标准 Laravel 代码)中的方法 beginDatabaseTransaction()
正在执行,但之后没有任何表存在。就好像正在创建数据库但没有执行迁移。
<?php
namespace Illuminate\Foundation\Testing;
trait DatabaseTransactions
{
/**
* Handle database transactions on the specified connections.
*
* @return void
*/
public function beginDatabaseTransaction()
{
$database = $this->app->make('db');
foreach ($this->connectionsToTransact() as $name) {
$database->connection($name)->beginTransaction();
}
$this->beforeApplicationDestroyed(function () use ($database) {
foreach ($this->connectionsToTransact() as $name) {
$connection = $database->connection($name);
$connection->rollBack();
$connection->disconnect();
}
});
}
测试代码(最后显示的指令失败):
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\Passport\ClientRepository;
class PassportTest extends TestCase
{
use DatabaseTransactions;
public function test_basic_oauth_token_authentication(){
$clientRepository = new ClientRepository();
$client = $clientRepository->createPersonalAccessClient(
null, 'Test Personal Access Client', $this->baseUrl
);
命令行:
vendor/bin/phpunit tests/Feature/PassPortTest
错误消息:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients Caused by Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients Caused by PDOException: SQLSTATE[HY000]: General error: 1 no such table: oauth_clients
如何确保表已作为每个测试的一部分创建?
最佳答案
使用 RefreshDatabase
特征而不是 DatabaseTransactions
特征(如果您尝试在同一个类中使用这两个特征,它们会发生冲突)。
关于laravel - 在 Laravel PHPUnit 测试中使用内存 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51948267/