After finally getting my stupid simple test to pass, I have a feeling that I\'m not doing it correctly.
I have a SessionsController, that is responsible for displaying
You can use the setUp
method to declare any dependencies that are global for the entire test class. It's similar to the tearDown
method you're currently using:
public function setUp()
{
// This method will automatically be called prior to any of your test cases
parent::setUp();
$this->userMock = Mockery::mock('Glenn\Repositories\User\UserRepositoryInterface');
}
However that won't work if your set up for the mock differs between tests. For this case you can use a helper method:
protected function getAuthMock($isLoggedIn = false)
{
$authorizedUser = Mockery::mock('Illuminate\Auth\AuthManager');
$authorizedUser->shouldReceive('user')->once()->andReturn($isLoggedIn);
}
Then when you need the auth mock you can just call getAuthMock
. This can greatly simplify your tests.
However
I don't think you're testing your controller correctly. You shouldn't instantiate the controller object yourself, instead you should utilize the call
method which exists in Laravel's TestCase
class. Try checking out this article about testing Laravel Controllers by Jeffrey Way. I think you're looking to do something more along the lines of this in your test:
class SessionsControllerTest extends TestCase
{
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
Mockery::close();
}
public function test_logged_in_user_cannot_see_login_page()
{
// This will bind any instances of the Auth manager during
// the next request to the mock object returned from the
// function below
App::instance('Illuminate\Auth\Manager', $this->getAuthMock(true));
// Act
$this->call('/your/route/to/controller/method', 'GET');
// Assert
$this->assertRedirectedTo('/');
}
protected function getAuthMock($isLoggedIn)
{
$authMock = Mockery::mock('Illuminate\Auth\Manager');
$authMock->shouldReceive('user')->once()->andReturn($isLoggedIn);
return $authMock;
}
}