Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 空的拆卸方法使phpunit测试通过_Mysql_Laravel_Sqlite_Phpunit - Fatal编程技术网

Mysql 空的拆卸方法使phpunit测试通过

Mysql 空的拆卸方法使phpunit测试通过,mysql,laravel,sqlite,phpunit,Mysql,Laravel,Sqlite,Phpunit,**我在Laravel 5.3中有以下测试类,正如您可以看到的tearDown()方法为空,我在内存中使用sqlite进行测试。 现在测试已经通过,但是当我删除空的tearDown()方法时,它会抛出一个外键约束失败异常, 我使用sqlite进行测试,phpunit.xml配置如下。 我尝试将测试数据库更改为mysql,那时一切都很好(即使没有tearDown())。知道为什么没有tearDown()方法会导致错误吗? use Illuminate\Foundation\Testing\With

**我在Laravel 5.3中有以下测试类,正如您可以看到的
tearDown()
方法为空,我在内存中使用sqlite进行测试。 现在测试已经通过,但是当我删除空的
tearDown()
方法时,它会抛出一个
外键约束失败异常
, 我使用sqlite进行测试,
phpunit.xml
配置如下。 我尝试将测试数据库更改为mysql,那时一切都很好(即使没有
tearDown()
)。知道为什么没有tearDown()方法会导致错误吗?
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\Model;
use App\Country;

class PhoneTest extends TestCase
{
    use DatabaseMigrations;

    public function setUp()
    {
        parent::setUp();

        Schema::disableForeignKeyConstraints();

        $this->seed(LocationsSeeder::class);

        Schema::enableForeignKeyConstraints();
    }

    public function test_we_can_make_number_international()
    {
        $phoneService  = resolve('Phone');
        $international = $phoneService->makeInternational('0507639889', Country::find(1));

        return $this->assertEquals($international, '+971507639889');
    }


    protected function tearDown()
    {
        //
    }
}
环境: 拉威尔5.3, 测试数据库:sqlite-inmemory phpunit 4.8.36 php-7 PHPUnit配置



编辑:我在新数据库上的迁移工作正常,甚至可以启动和回滚

由于您使用的是
数据库迁移
特性,Laravel尝试执行
迁移:回滚
命令:

 $this->beforeApplicationDestroyed(function () {
     $this->artisan('migrate:rollback');
 });
而且,由于您的迁移很混乱(这就是为什么要关闭FK约束),所以它失败了

当您放置
tearDown()
方法时,您正在覆盖父类
illighted\Foundation\Testing\TestCase
类中的
tearDown()
方法,因此永远不会执行
beforeApplicationDestroyed
回调


处理此问题的最佳方法是在每次迁移中修复
down()
方法,以便在执行
migrate:rollback
命令时删除每个表。但您也可以从
tearDown()
方法运行。或者使用
disableForeignKeyConstraints()
禁用FK约束,就像在
setUp()
方法中所做的那样

但是有一件事,当我将测试数据库更改为mysql时,为什么会这样呢?谢谢@Alexy Mezenin的帮助
 $this->beforeApplicationDestroyed(function () {
     $this->artisan('migrate:rollback');
 });