gpt4 book ai didi

unit-testing - 在 laravel 测试中调用命名路由

转载 作者:行者123 更新时间:2023-11-28 19:47:22 26 4
gpt4 key购买 nike

考虑以下几点:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class HomeRouteTest extends TestCase
{
public function testVisitTheHomePage()
{
$response = $this->call('GET', '/');
$this->assertEquals(200, $response->status());

}

public function testVisitTheAboutPage()
{
$response = $this->call('GET', '/about');
$this->assertEquals(200, $response->status());
}
}

有没有,我没有看到文档 >.>,做类似的事情:

$response = $this->call('GET', 'home.about');
$this->assertEquals(200, $response->status());

或者......你是这样做的吗?

我得到的错误是:

vagrant@scotchbox:/var/www$ phpunit
PHPUnit 4.8.21 by Sebastian Bergmann and contributors.

FF

Time: 3.41 seconds, Memory: 14.25Mb

There were 2 failures:

1) HomeRouteTest::testVisitTheHomePage
Failed asserting that 404 matches expected 200.

/var/www/tests/HomeRouteTest.php:12

2) HomeRouteTest::testVisitTheAboutPage
Failed asserting that 404 matches expected 200.

/var/www/tests/HomeRouteTest.php:19

FAILURES!
Tests: 2, Assertions: 2, Failures: 2.

最佳答案

据我所知,此解决方案适用于任何 Laravel 5 版本。特别是 Laravel 5.4+ 与此处提到的其他解决方案不同。

如果你的命名路由有参数,你可以这样做:

$response = $this->get(route('users.show', [
'user' => 3,
]));

$response->assertStatus(200);

如果你的命名路由没有参数那么你可以这样做:

$response = $this->get(route('users.index'));

$response->assertStatus(200);

漂亮而简单。

关于unit-testing - 在 laravel 测试中调用命名路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com