TDD 构建 Laravel 应用之创建项目视图

本系列文章为 laracasts.com 的系列视频教程 ——Build A Laravel App With TDD 的学习笔记,若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版

游客无法操作项目

之前的很多测试都是在用户登录的状态下写的测试用例,现在我们来写一份游客相关的测试:

1
2
3
4
5
6
7
8
9
/** @test */
public function guests_cannot_manage_projects()
{
$project = factory('App\Project')->create();

$this->get('/projects')->assertRedirect('login');
$this->get($project->path())->assertRedirect('login');
$this->post('/projects', $project->toArray())->assertRedirect('login');
}

运行测试成功!

创建项目视图

目前我们还没有创建项目视图,实际项目中应该有一个创建项目的页面。让我们先从测试开始:

1
2
3
4
5
6
7
8
9
10
11
/** @test */
public function a_user_can_create_a_project()
{
$this->withoutExceptionHandling();

$this->actingAs(factory('App\User')->create());

$this->get('/projects/create')->assertStatus(200);

...
}

运行测试结果如下:

Tests\Feature\ManageProjectsTest::a_user_can_create_a_project
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Project] create

增加路由:

1
2
3
4
5
6
Route::group(['middleware' => 'auth'], function () {
Route::get('/projects', 'ProjectsController@index');
Route::get('/projects/create', 'ProjectsController@create');
Route::get('/projects/{project}', 'ProjectsController@show');
Route::post('/projects', 'ProjectsController@store');
});

运行测试结果如下:

Tests\Feature\ManageProjectsTest::a_user_can_create_a_project
BadMethodCallException: Method App\Http\Controllers\ProjectsController::create does not exist.

增加方法:

1
2
3
4
public function create()
{

}

运行测试成功!

创建视图文件:

1
2
3
4
public function create()
{
return view('projects.create');
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.bootcss.com/bulma/0.7.5/css/bulma.css" rel="stylesheet">
<title>Birdboard</title>
</head>
<body>
<form method="post" action="/projects" class="container" style="padding-top: 40px">
@csrf
<h1 class="heading is-1">Create a Project</h1>
<div class="field">
<label class="label" for="title">Title</label>

<div class="control">
<input type="text" class="input" name="title" placeholder="Title" id="title">
</div>
</div>
<div class="field">
<label class="label" for="description">Description</label>

<div class="control">
<textarea class="textarea" name="description" placeholder="Description" id="description">
</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link">Create Project</button>
</div>
</div>
</form>
</body>
</html>

在游客无法操作项目中新增创建视图测试:

1
2
3
4
5
6
7
8
9
10
/** @test */
public function guests_cannot_manage_projects()
{
$project = factory('App\Project')->create();

$this->get('/projects')->assertRedirect('login');
$this->get('/projects/create')->assertRedirect('login');
$this->get($project->path())->assertRedirect('login');
$this->post('/projects', $project->toArray())->assertRedirect('login');
}

运行测试成功!