我的第一步是检查/ home控制器是否在主页上被调用.
尽管接下来的几个教程,我无法让测试工作,我看不出我做错了什么.
我的设置是:
作曲家安装laravel
作曲家安装PHPunit
这是我的路线:
<?PHP
Route::get('/login','AuthenticationController@login');
我的控制器:
<?PHP
class AuthenticationController extends BaseController {
public function login () {
return View::make('authentication.login');
}
}
我的测试:
<?PHP
class AuthenticationTest extends TestCase {
public function testSomeTest () {
$response = $this->action('GET','AuthenticationController@login');
$view = $response->original;
$this->assertEquals('authentication.login',$view['name']);
}
}
我得到的错误是
ErrorException: Undefined index: name
代码作为副本(几乎完全是从Laravel站点),但它不运行.
谁能看到我在做错什么?
它声称$view没有索引名称,但是不能像Laravel网站上的示例那样正确,加上使用其名称渲染视图(在前端也正确显示)
编辑::
所以从评论看来,laravel单元测试部分不清楚,$view [‘name’]正在检查名为$name的变量.如果是这样,那么你如何测试使用的控制器/路由,IE.什么控制器名称/动作名称已被用于路由(‘X’)
“My first step is to check that the /login controller is called on the home url.”
所以这意味着:当用户点击主路由时,您需要检查用户是否登录,如果不是,您需要将其重定向到登录名,或许有一些Flash消息.登录后,您要将其重定向到主页.如果登录失败,您需要将它们重定向到登录表单,也可能使用Flash消息.
所以有几件事要测试:家用控制器和登录控制器.所以遵循TDD的精神,让我们先创建测试.
注意:我将遵循phpspec使用的一些命名约定,但不要让你烦恼.
class HomeControllerTest extends TestCase
{
/**
* @test
*/
public function it_redirects_to_login_if_user_is_not_authenticated()
{
Auth::shouldReceive('check')->once()->andReturn(false);
$response = $this->call('GET','home');
// Now we have several ways to go about this,choose the
// one you're most comfortable with.
// Check that you're redirecting to a specific controller action
// with a flash message
$this->assertRedirectedToAction(
'AuthenticationController@login',null,['flash_message']
);
// Only check that you're redirecting to a specific URI
$this->assertRedirectedTo('login');
// Just check that you don't get a 200 OK response.
$this->assertFalse($response->isOk());
// Make sure you've been redirected.
$this->assertTrue($response->isRedirection());
}
/**
* @test
*/
public function it_returns_home_page_if_user_is_authenticated()
{
Auth::shouldReceive('check')->once()->andReturn(true);
$this->call('GET','home');
$this->assertResponSEOk();
}
}
这就是家庭控制器.在大多数情况下,您实际上不在乎您被重定向到哪里,因为这可能会随时间而变化,您将不得不更改测试.所以至少应该做的是检查你是否被重定向,只有检查更多的细节,如果你真的认为这是重要的测试.
我们来看看认证控制器:
class AuthenticationControllerTest extends TestCase
{
/**
* @test
*/
public function it_shows_the_login_form()
{
$response = $this->call('GET','login');
$this->assertTrue($response->isOk());
// Even though the two lines above may be enough,// you Could also check for something like this:
View::shouldReceive('make')->with('login');
}
/**
* @test
*/
public function it_redirects_back_to_form_if_login_fails()
{
$credentials = [
'email' => 'test@test.com','password' => 'secret',];
Auth::shouldReceive('attempt')
->once()
->with($credentials)
->andReturn(false);
$this->call('POST','login',$credentials);
$this->assertRedirectedToAction(
'AuthenticationController@login',['flash_message']
);
}
/**
* @test
*/
public function it_redirects_to_home_page_after_user_logs_in()
{
$credentials = [
'email' => 'test@test.com',];
Auth::shouldReceive('attempt')
->once()
->with($credentials)
->andReturn(true);
$this->call('POST',$credentials);
$this->assertRedirectedTo('home');
}
}
再次,总是想想你真正想要测试的内容.你真的需要知道在哪个路由上触发哪个控制器动作?或者视图的名称被返回?实际上,您只需要确保控制器实际上尝试这样做.您传递一些数据,然后测试它是否按预期行为.
并且始终确保您不尝试测试任何框架功能,例如,如果特定路由触发特定操作或视图正确加载.这已经被测试了,所以你不需要担心.专注于您的应用程序的功能,而不是基础框架.