phpunit tests 500 status confusion with multiple guards
What my test is trying to do, is test if an admin can visit the "profile" route, and the intended behavior is they should be redirected to the "login" page so they can login as a user. Important to note is that an admin is a different guard than a user.
The test currently fails, with the message that "Response status code [500] is not a redirect status code." I'm not really sure how I should deal with this issue.
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\User;
use App\Admin;
use App\SuperAdmin;
class AccountPageTest extends TestCase
{
use DatabaseTransactions;
/** My other tests.... **/
/** @test **/
public function admins_cannot_access_page()
{
$this->refreshApplication();
$admin = Admin::find(1);
$response = $this->actingAs($admin, 'admin')
->get('profile')
->assertRedirect('login');
}
}
@lemmon@tykus I'll give your suggestions a try, and I have Admin and User user types because I want only admins to be able to use the admin panel to manage the website.
It doesn't seem to make a difference unfortunately.
Tests\Feature\AccountPageTest::admins_cannot_access_page
Response status code [500] is not a redirect status code.
Failed asserting that false is true.
/** @test **/
public function admins_cannot_access_page()
{
$this->withoutExceptionHandling();
$this->refreshApplication();
$admin = Admin::find(1);
$response = $this->actingAs($admin, 'admin')
->get('profile')
->assertRedirect('login');
}
I changed the order of the first two statements and get a different error. (It might just be something I should work out on my own)
Tests\Feature\AccountPageTest::admins_cannot_access_page
TypeError: Argument 1 passed to App\Http\Controllers\ProfilesController::getBillingAddressString() must be an instance of App\Profile, instance of App\AdminProfile given, called in C:\Users\Owner\Documents\WebDevelopment\ReachWebExperts_Github\ReachWebExpertsProject\app\Http\Controllers\ProfilesController.php on line 24
/** @test **/
public function admins_cannot_access_page()
{
$this->refreshApplication();
$this->withoutExceptionHandling();
$admin = Admin::find(1);
$response = $this->actingAs($admin, 'admin')
->get('profile')
->assertRedirect('login');
}
If I change ->assertRedirect('login'); to ->assertRedirect(''); then the test passes. But I know that the admin is redirected to /login page because I tested it in my own browser. I'm not sure what's behind this weird behavior.