I am trying to start a new Laravel app following TDD
My first step is to check that the /login controller is called on the home url.
Despite following several tutorials I can't get the test to work and I can't see what I'm doing wrong at all.
My set up is: composer to install laravel composer to install phpunit
here is my route:
<?php
Route::get('/login', 'AuthenticationController@login');
my controller:
<?php
class AuthenticationController extends BaseController {
public function login () {
return View::make('authentication.login');
}
}
And my test:
<?php
class AuthenticationTest extends TestCase {
public function testSomeTest () {
$response = $this->action('GET', 'AuthenticationController@login');
$view = $response->original;
$this->assertEquals('authentication.login', $view['name']);
}
}
The error I get is
ErrorException: Undefined index: name
The code as a copy (pretty much exactly) from the Laravel site, yet it doesn't run.
Can anyone see what I'm doing wrong?
It claims $view has no index name, but that can't be right as its the example on the laravel website, plus the view is being rendered using its name (it is showing correctly on the front end too)
EDIT::
So it seems from a comment that the laravel unit testing section isn't clear and that the $view['name'] is checking for a variable called $name. If that is the case, how do you test the controller/route used, IE. what controller name/action name has been used for route('X')
Ok, as already explained a little in the comments, let's first take a step back and think about the scenario.
So that means: When the user hits the home route, you want to check if the user is logged in. If he's not, you want to redirect them to the login, maybe with some flash message. After they have logged in, you want to redirect them back to the home page. If the login fails, you want to redirect them back to the login form, maybe also with a flash message.
So there are several things to test now: The home controller and the login controller. So following the TDD spirit, let's create the tests first.
Note: I'll follow some naming convention that is used by phpspec, but don't let that bother you.
And that's it for the Home controller. In most cases you actually don't care where you are redirected to, because that may change over time and you would have to change the tests. So the least you should do is to check whether you are being redirected or not and only check for more details if you really think that it matters for your test.
Let's have a look at the Authentication controller:
Again, always think about what you really want to test. Do you really need to know which controller action is triggered on which route? Or what the name of the view is that is returned? Actually, you just need to make sure that the controller actually attempts to do it. You pass it some data and then test if it behaves as expected.
And always make sure you're not trying to test any framework functionality, for instance if a specific route triggers a specific action or if a View is loaded correctly. This has already been tested, so you don't need to worry about that. Focus on the functionality of your application and not on the underlying framework.