laravel 进行单元测试报如下错
vendor\bin\phpunit --filter a_thread_belongs_to_a_channel
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 577 ms, Memory: 14.00MB
There was 1 error:
1) Tests\Unit\ThreadTest::a_thread_belongs_to_a_channel
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL (SQL: alter table "threads" add column "channel_id" integer not null)
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:647
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:607
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:450
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\Blueprint.php:86
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php:239
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php:148
D:\laragon323\www\forum\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:221
D:\laragon323\www\forum\database\migrations\2018_06_08_102026_alter_t
查阅了相关资料,尝试了给加默认值方案者不行
Schema::table('organisations', function (Blueprint $table){
$table->string('long_name')->nullable()->after('default_value');
});
最终如下执行,解决了。
php artisan config:cache
ok! 找到正解!!!
原因
貌似开启了缓存,在错误的数据库中运行。实际上是需要忽略测试连接,因本地用的是mysql库,而测试环境下使用的内存级数据库:memory:
sqlite
为了解决这个原始错误,我刚刚创建了一个MySQL数据库进行测试。用AppyEnv=测试创建一个.nv.file文件,然后在PHPUnIT.xml中更改了Env。
<env name="APP_ENV" value="testing"/>
再次,确定有否安装相关的composer包
composer require doctrine/dbal --dev