我用Laravel Dusk编写了测试,无需清除数据库中以前的测试数据,所以一切正常。
在 DuskTestCase.php 文件中添加use DatabaseMigrations;
时,我的问题就出现了,以确保清除所有以前的测试数据。
但问题是登录不起作用。所以很多测试都失败了,因为我的测试被重定向到应用程序登录页面!
任何帮助将不胜感激。
这是我的测试代码:
<?php
namespace Tests\Browser;
use App\Models\Brand;
use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\Browser\Components\DashboardCategoryForm;
use Tests\DuskTestCase;
class DashboardBrandsTest extends DuskTestCase
{
/**
* A Dusk test example.
*
* @return void
* @throws \Exception
* @throws \Throwable
*/
public function testBrandList()
{
$user = factory(User::class)->create();
$this->browse(function (Browser $browser) use ($user) {
$browser->loginAs($user)
->visitRoute('dashboard.brands.index')
->assertSee('Brands')
->assertSee('id')
->assertSee('name')
->assertSee('image')
->assertSee('updated_at')
->assertSee('operations');
});
}
/**
* @throws \Exception
* @throws \Throwable
*/
public function testCreateUpdateDeleteBrand()
{
$user = factory(User::class)->create();
$this->browse(function (Browser $browser) use ($user) {
$expectedName = 'Brand ' . $user->id;
$expectedNameEdited = $expectedName . ' Edited';
$expectedShortDesc = 'This is a little desc for test.';
$expectedShortDescEdited = 'Edited desc';
$expectedImage = __DIR__ . '/photos/test.jpg';
$browser->loginAs($user)
->visitRoute('dashboard.brands.index')
->click('@new-category')
->assertRouteIs('dashboard.brands.create')
->within(new DashboardCategoryForm, function ($browser)
use ($expectedName, $expectedShortDesc, $expectedImage) {
$browser->fillCategory(
$expectedName,
$expectedShortDesc,
$expectedImage
);
})
->click('@submit')
->assertRouteIs('dashboard.brands.index')
->assertSee($expectedName);
$latestRecord = Brand::orderBy('updated_at', 'desc')->first();
$browser
->click('@edit-category' . $latestRecord->id)
->within(new DashboardCategoryForm, function ($browser)
use ($expectedName, $expectedShortDesc) {
$browser->checkCategory(
$expectedName,
$expectedShortDesc
);
})
->within(new DashboardCategoryForm, function ($browser)
use ($expectedNameEdited, $expectedShortDescEdited, $expectedImage) {
$browser->fillCategory(
$expectedNameEdited,
$expectedShortDescEdited,
$expectedImage
);
})
->click('@submit')
->assertRouteIs('dashboard.brands.index')
->assertSee($expectedNameEdited)
->click('@edit-category' . $latestRecord->id)
->within(new DashboardCategoryForm, function ($browser)
use ($expectedNameEdited, $expectedShortDescEdited) {
$browser->checkCategory(
$expectedNameEdited,
$expectedShortDescEdited
);
})
->click('@submit')
->assertRouteIs('dashboard.brands.index')
->click('@delete-category' . $latestRecord->id)
->acceptDialog()
->pause(500)
->assertDontSee($expectedNameEdited);
});
}
}
这是我的env.dusk.local:
APP_NAME=Laravel
APP_ENV=testing
APP_KEY=base64:EOMuHiEoMcS0YvJQq+b/bSVm0kXfJwNnNFJ7WOQwWwQ=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://site.local
DB_CONNECTION=mysql
DB_HOST=10.5.0.7
DB_PORT=3306
DB_DATABASE=db_test
DB_USERNAME=root
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=database
SESSION_DRIVER=database
SESSION_LIFETIME=120
QUEUE_DRIVER=sync
REDIS_HOST=10.5.0.9
REDIS_PASSWORD=secret
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
答案 0 :(得分:0)
当您开始编辑类别时,这意味着您预计数据库中已存在数据。 DatabaseMigrations创建了您的数据库结构,但它并没有为您的数据库设定种子,因此如果您尝试登录,您的数据库中可能没有用户登录。
我发现解决此问题的最佳方法是在每次测试开始时添加种子,但这确实会让它们变得很慢。我尝试了很多方法,这是我唯一可以上班的方式。
public function setUp()
{
parent::setUp();
$this->artisan('db:seed');
}