The*_*ebs 6 testing phpunit unit-testing laravel-5 laravel-5.2
考虑以下:
<?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());
}
}
Run Code Online (Sandbox Code Playgroud)
离开了,而不是我见过记录>.>,做类似的事情:
$response = $this->call('GET', 'home.about');
$this->assertEquals(200, $response->status());
Run Code Online (Sandbox Code Playgroud)
或者....你是怎么做到的?
我得到的错误是:
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.
Run Code Online (Sandbox Code Playgroud)
小智 7
据我所知,此解决方案适用于任何 Laravel 5 版本。尤其是 Laravel 5.4+ 与这里提到的其他解决方案不同。
如果你的命名路由有参数,你可以这样做:
$response = $this->get(route('users.show', [
'user' => 3,
]));
$response->assertStatus(200);
Run Code Online (Sandbox Code Playgroud)
如果您的命名路由没有参数,那么您可以这样做:
$response = $this->get(route('users.index'));
$response->assertStatus(200);
Run Code Online (Sandbox Code Playgroud)
好看又简单。
小智 5
这是一个很晚的回复,但我认为您正在寻找的是:
$this->route('GET', 'home.about');
$this->assertResponseOk(); // Checks that response status was 200
Run Code Online (Sandbox Code Playgroud)
对于带参数的路由,可以这样做:
$this->route('GET', 'users.show', ['id' => 3]); // (goes to '/users/3')
Run Code Online (Sandbox Code Playgroud)
我需要这个来测试我正在使用的一些可怕的路线,如下所示:
Route::resource(
'/accounts/{account_id}/users',
'AccountsController@users',
[
'parameters' => ['account_id'],
'names' => [
'create' => 'users.create',
'destroy' => 'users.destroy',
'edit' => 'users.edit',
'show ' => 'users.show',
'store' => 'users.store',
'update' => 'users.update',
]
]
);
Run Code Online (Sandbox Code Playgroud)
为了确保我的路线和重定向到达正确的页面,我这样做了:
/**
* @test
*/
public function users_index_route_loads_correct_page()
{
$account = Account::first();
$response = $this->route('get', 'users.index', ['account_id' => $account->id]);
$this->assertResponseOk()
->seePageIs('/accounts/' . $account->id . '/users');
}
Run Code Online (Sandbox Code Playgroud)
为了确保我使用正确的视图文件(我们都会犯复制粘贴错误,对吧?),我这样做了:
/**
* @test
*/
public function users_index_route_uses_expected_view()
{
$account = Account::first();
$response = $this->route('get', 'users.index', ['account_id' => $account->id]);
$view = $response->original; // returns View instance
$view_name = $view->getName();
$this->assertEquals($view_name, 'users.index');
}
Run Code Online (Sandbox Code Playgroud)
如果不是 PHPStorm 中的 Structure 功能和我偶然发现的 Laravel 4.2 文档,我想我永远不会弄清楚如何测试这些路由。我希望这可以节省其他人一些时间。