问题描述
我现在正在测试我的Laravel应用程序,并面临下一个问题:我的项目是从无框架的PHP项目迁移而来的,该项目已经拥有自己的数据库,因此我无法迁移大多数db数据.在测试期间,我需要刷新数据库,但是到目前为止,当我使用 use RefreshDatabase;
时,我的整个数据库正在刷新(因此表也将被删除),因此laravel认为,就是我每次都在迁移数据库表,这是我没有做的事情.因此,问题是:每个新测试是否可以仅刷新记录(我的意思是删除它们),而不刷新整个数据库?我已经用谷歌搜索了,不幸的是没有找到东西,这可能会有所帮助.预先感谢!
I am tesing now my Laravel app and facing the next problem: my project was migrated from a no-framework PHP project, which already has its own database, so I do no have migrations for most of db data. During the testing I need to refresh my database, but as far as I now, when I use use RefreshDatabase;
my whole database is being refreshed(so the tables are being dropped as well), so laravel supposes, that I am migrating my db tables every time, what I am not doing. So the question is: is it possible for every new test to refresh just the records( I mean delete them), but not the whole database ? I've googled it and unfortunately did not find something, which might help. Thanks in advance!
推荐答案
如果您使用的是 Phpunit
,则可以使用 DatabaseTransactions
,它将添加到数据库中您在测试中添加的内容然后将其删除.就像特质一样使用它.
If you are using Phpunit
you can use DatabaseTransactions
it will add to database what you added in tests and after that it will delete it. Just use it like trait.
如果您不想创建测试数据库,可以使用以下简单方法:
If you don't want to create testing db it's easy way like this:
<?php
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
use DatabaseTransactions;
...
您可以在此处找到它.
这篇关于在测试期间重置Laravel中数据库中的记录,但不重置数据库本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!