alexlegard's avatar

Unexpected 500 status in test

In my test, I'm expecting a 200 status, not 500. I can go to the page in question just fine in my localhost. So it seems to be a test problem, not a problem with the website. I just want to use one of my existing admins in the database and use that for the test, not using fakers or any of that.

I have a very similar question from back in my post history, but it doesn't seem to be the same solution.

Anyway, the test method in question:


namespace Tests\Feature\admin;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use App\Admin;
use App\User;
use App\SuperAdmin;
use App\Dish;

class AddSelectionPageTest extends TestCase
{
    use DatabaseTransactions;
    use WithoutMiddleware;

    /** @test **/
    public function add_new_selection_page_loads_correctly()
    {
        $this->refreshApplication();

        $admin = Admin::find(1);

        $response = $this->actingAs($admin, 'admin')
            ->get('admin/my-dishes/1/selections/add') // Works just fine if I shorten this to 'admin/my-dishes'
            ->assertStatus(200);
    }

I also have different tests in different files that look very similar, and they work fine. For instance-


use DatabaseTransactions;
use WithoutMiddleware;

/** @test **/
    public function admin_can_access_page()
    {
        $this->refreshApplication();

        $admin = Admin::find(1);

        $response = $this->actingAs($admin, 'admin')
            ->get('admin/my-restaurants/create')
            ->assertStatus(200)
            ->assertSee('Apply to add a new res');
    }

0 likes
6 replies
Tray2's avatar

What does the 500 error say?

If it doesn't say anything useful. try adding $this->withoutExceptionHandling(); at the top of your test.

alexlegard's avatar

@tray2 Oh, in the phpunit output it just gives Expected response status code [200] but received 500.

Tray2's avatar
Tray2
Best Answer
Level 74

@alexlegard Then try adding the withoutExceptionHandling call, it turns off Laravels exception handling and gives better errors. You have a code error somewhere in your controller, model or view.

alexlegard's avatar

@tray2 I tried it and I guess it makes things a little more clear. It's a bug in my AuthServiceProvider?


App\Providers\AuthServiceProvider::App\Providers\{closure}(): Argument #1 ($user) must be of type ?App\User, App\Admin given, called in C:\Users\Owner\Documents\WebDevelopment\ReachWebExperts_Github\ReachWebExpertsProject\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 536

  at C:\Users\Owner\Documents\WebDevelopment\ReachWebExperts_Github\ReachWebExpertsProject\app\Providers\AuthServiceProvider.php:42
     38▕                $this->registerPolicies();
     39▕
     40▕                /************* ADMIN GATES *************/
     41▕                //Restaurants
  ➜  42▕                Gate::define('owns-restaurant', function (User $user = null, Restaurant $restaurant) {
     43▕
     44▕                        if( Auth::guard('admin')->check() ) {
     45▕                                $admin = Auth::guard('admin')->user();
     46▕                                return $restaurant->admins->contains($admin->id);

  1   C:\Users\Owner\Documents\WebDevelopment\ReachWebExperts_Github\ReachWebExpertsProject\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php:536
      App\Providers\AuthServiceProvider::App\Providers\{closure}(Object(App\Admin), Object(App\Restaurant))

  2   C:\Users\Owner\Documents\WebDevelopment\ReachWebExperts_Github\ReachWebExpertsProject\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php:431
      Illuminate\Auth\Access\Gate::callAuthCallback(Object(App\Admin), "owns-restaurant")

alexlegard's avatar

@tray2 Thank you, I replaced User $user = null with $user = null and it works okay now.

alexlegard's avatar

@tray2 Awesome, I had nine different test errors that all cleared up after fixing that one bug.

Please or to participate in this conversation.