使用 Laravel 5 [phpunit] 测试方法和控制器
Testing methods and controllers with Laravel 5 [phpunit]
我正在尝试为我的项目做测试,谁能告诉我如何编写测试的例子,或者给我一些好的视频课程来学习测试。
在我的示例中,我试图测试这部分:
public function getProjectsForUser(){
if(Auth::user() -> admin_id == NULL){
$this->id = Auth::user() -> id;
}else{
$this->id = Auth::user() -> admin_id;
}
$projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();
$role = User::findOrFail(Auth::user() -> id)->roles;
$users = User::where('admin_id', '=', $this->id)->lists('name','id');
return array('projects' => $projects,'users' => $users,'roles' => $role );
}
这是我的模型Project.php
这是我的 PorojectController:
public function project(Project $projects){
$this -> projects = $projects ->getProjectsForUser();
return view('projects',$this -> projects);
}
这是我检查用户是否登录的测试...
public function testHome()
{
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
}
现在我需要检查我是否在数组中为 $project、$role、$users 获取了正确的值。
您可以使用 assertViewHas()
断言本应传递给视图的值。例如:
public function testHome(){
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
$this->assertViewHas('projects', 'expected value for projects');
$this->assertViewHas('users', 'expected value for users');
$this->assertViewHas('roles', 'expected value for roles');
}
顺便说一下:如果您没有传递第二个参数,该方法将只断言变量存在但不会比较值。
要使用稍微不同的语法来完成相同的操作,您可以使用 assertViewHasAll()
并将所有内容作为数组传递:
$this->assertViewHasAll([
'projects' => 'expected value for projects',
'users' => 'expected value for users',
'roles' => 'expected value for roles'
]);
我正在尝试为我的项目做测试,谁能告诉我如何编写测试的例子,或者给我一些好的视频课程来学习测试。
在我的示例中,我试图测试这部分:
public function getProjectsForUser(){
if(Auth::user() -> admin_id == NULL){
$this->id = Auth::user() -> id;
}else{
$this->id = Auth::user() -> admin_id;
}
$projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();
$role = User::findOrFail(Auth::user() -> id)->roles;
$users = User::where('admin_id', '=', $this->id)->lists('name','id');
return array('projects' => $projects,'users' => $users,'roles' => $role );
}
这是我的模型Project.php
这是我的 PorojectController:
public function project(Project $projects){
$this -> projects = $projects ->getProjectsForUser();
return view('projects',$this -> projects);
}
这是我检查用户是否登录的测试...
public function testHome()
{
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
}
现在我需要检查我是否在数组中为 $project、$role、$users 获取了正确的值。
您可以使用 assertViewHas()
断言本应传递给视图的值。例如:
public function testHome(){
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
$this->assertViewHas('projects', 'expected value for projects');
$this->assertViewHas('users', 'expected value for users');
$this->assertViewHas('roles', 'expected value for roles');
}
顺便说一下:如果您没有传递第二个参数,该方法将只断言变量存在但不会比较值。
要使用稍微不同的语法来完成相同的操作,您可以使用 assertViewHasAll()
并将所有内容作为数组传递:
$this->assertViewHasAll([
'projects' => 'expected value for projects',
'users' => 'expected value for users',
'roles' => 'expected value for roles'
]);