【发布时间】:2022-01-10 18:39:05
【问题描述】:
我正在使用 Laravel Dusk 测试页面上的所有链接,这些链接是 PDF 文件,它们通过 laravel mix 从资源/文档复制到公共/文档。
问题是尝试使用 get(uri) 方法访问文档的 URL 时会出现 404,这是我 Dusk 的简化版本:
protected function refreshApplicationWithLocale($locale)
{
self::tearDown();
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
self::setUp();
}
protected function setUp(): void
{
parent::setUp();
if(!File::copyDirectory( resource_path('docs'), public_path() . '/docs'))
throw new \Exception("couldn't copy the folder :(");
// Storage::assertExists(public_path() . '/docs/clothing/existing_file.pdf'); // this assertion fails, what's weird is the path in the exception message does actually exist
$this->artisan('storage:link');
}
protected function tearDown(): void
{
parent::tearDown();
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
}
public function testClothing()
{
$this->refreshApplicationWithLocale('fr');
$this->get('/test')->assertStatus(200); // passes
$this->get('/fr/vetements')->assertStatus(200); // passes
//usually I'd get the page urls dynamically with Dusk, but for simplicity sake I'm just testing with this link that I know it exists and can curl it from withing Homestead
$this->get('/docs/clothing/Hoodies.pdf')->assertStatus(200); //failes (404)
}
我错过了什么?
谢谢。
P.S.:我正在使用 laravel 8 和 Homestead
编辑:
在@steven7mwesigwa 回答之后,这是我的最终代码:
protected function refreshApplicationWithLocale($locale)
{
self::tearDown();
putenv(LaravelLocalization::ENV_ROUTE_KEY . '=' . $locale);
self::setUp();
}
protected function setUp(): void
{
parent::setUp();
if(!File::copyDirectory( resource_path('docs'), public_path() . '/docs'))
throw new \Exception("couldn't copy the documents folder :(");
}
protected function tearDown(): void
{
parent::tearDown();
foreach (static::$browsers as $browser) {
$browser->driver->manage()->deleteAllCookies();
}
}
public function testClothing()
{
$this->refreshApplicationWithLocale('fr');
$elements = [];
$this->browse(function (Browser $browser) use(&$elements){
$elements = $browser->visit('myroute')
->elements('.tmp_products_list a', 'href');
});
$this->assertGreaterThan(1, count($elements));
foreach($elements as $element) {
$response = Http::get($element->getAttribute('href'));
self::assertEquals($response->status(), 200);
}
}
【问题讨论】:
标签: laravel testing homestead laravel-dusk laravel-testing