laravel - 因为测试不好用,laravel中的phpunit?
问题描述
我有以下设置:
配置/数据库.php
特别是在:'连接'
'testing' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('TEST_DB_HOST'),
'port' => env('TEST_DB_PORT', '5432'),
'database' => env('TEST_DB_DATABASE', 'forge'),
'username' => env('TEST_DB_USERNAME', 'forge'),
'password' => env('TEST_DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer'
],
在 .env 中
TEST_DB_CONECTION=testing
TEST_DB_HOST=mysql-db
TEST_DB_PORT=3306
TEST_DB_DATABASE=db_test
TEST_DB_USERNAME=root
TEST_DB_PASSWORD=secret_test
我所有的文件phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value="db_test"/>
</php>
</phpunit>
在我的测试班里
use RefreshDatabase;
protected $user;
public function setUp() : void {
parent::setUp();
$this->user = factory(User::class)->create();
$this->actingAs($this->user);
}
public function testCreateExcessCarriers(){
var_dump("some text to try");
}
并在执行时:
docker-compose exec utraq vendor/bin/phpunit tests/Unit/Http/Controllers/ECControllerTest.php --filter testCreateExcessCarriers
我收到此错误:
Illuminate\Database\QueryException:SQLSTATE[HY000]:一般错误:1 无法添加默认值为 NULL 的 NOT NULL 列(SQL:alter table "users" add column "agency_id" integer not null)
这样我已经配置了工厂,用模型检查一切都很好
$factory->define(App\Models\User::class, function (Faker $faker) {
static $password;
return [
'uuid' => $faker->uuid,
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'active' => 1,
'agency_id' => 3,
];
});
但是我不知道为什么在执行命令后会出现该错误。我该如何纠正?
编辑: 我猜我得到错误的迁移。对于用户表,有 2 个迁移。
迁移 1:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('uuid');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('address1')->nullable(); //Address 1
$table->string('address2')->nullable(); //Address 2
$table->string('city')->nullable(); //City
$table->string('state_name')->nullable(); //State
$table->integer('state_id')->nullable(); //State
$table->string('zip_code', 10)->nullable(); //Zip Code
$table->string('employee_type')->nullable();
$table->dateTime('last_logged_in')->nullable();
$table->json('dashboard')->nullable();
$table->rememberToken();
$table->timestamps();
});
迁移 2:如您所见,这agency_id
是分配字段的位置
Schema::table('users', function (Blueprint $table) {
$table->integer('agency_id')->after('password');
$table->boolean('active')->after('remember_token')->default('0');
$table->boolean('verified')->after('active')->default(false);
$table->string('avatar')->nullable();
});
解决方案
根据您的问题,我想您实际上想使用您定义为“测试”连接的 mysql 测试数据库。
但是在您的 phpunint 配置中,您使用
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value="db_test"/>
这显然与 sqlite 连接有关。其他人已经指出了 sqlite 的问题并添加了不可为空的列
您可能想要更改连接并删除 DB_DATABASE 条目
<env name="DB_CONNECTION" value="testing"/>
编辑:
TEST_DB_CONECTION=testing
似乎没有在任何地方使用?还有一个错字:) - 也可能从你的环境中删除
推荐阅读
- angularjs - Angular JS 的 TSLint - 设置、配置和实现
- laravel - Laravel 同一张表的关系列
- django - 如何在django中计算视频长度和视频缩略图
- angularjs - 需要将 Angular js 迁移到 Angular 的步骤
- python - 我的 while 循环有问题,不太清楚为什么
- python - 从大型时间序列数据集中随机抽样
- d3.js - 初学者 d3.js 用户 - 如何从一个 JSON 文件中绘制多个路径?
- javascript - 在 reactjs 商店中找到不可调用的 @@iterator
- node.js - 续集循环依赖?
- rest - WCF Rest Service Sharepoint 托管提供 401 未经授权