4

i'm trying to perform a test to check if

1 - the user is authenticated

2 - has permission to view the requested page

every time i run the test i get this error

Expected status code 200 but received 302.

namespace Tests\Feature\Category;

class CategoryTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function it_can_see_all_categories()
{
    $user = $this->signedIn();

    $permission = Permission::create(['group' => 'categories' , 'name' => 'view categories' , 'label' => 'view categories']);

    $role = Role::find($user->role_id);

    $role->givePermissionTo($permission);

    $response = $this->get('/categories');

    $response->assertStatus(200 , $response->getStatusCode());
  }
}

My Controller

<?php
namespace App\Http\Controllers\Category;

class CategoriesController extends Controller   
{
protected $categRepo;

/**
 * Initializing Categories Repository
 */

public function __construct(CategoryRepository $categRepo)
{
    $this->categRepo = $categRepo;
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(ManageCategoryRequest $request)
{
    $categories = $this->categRepo->getPaginated(25);
    return view('categories.index' , compact('categories'));
}
}     

And here is My Request For Request Authorization

<?php

class ManageCategoryRequest extends FormRequest
{
     public function authorize()
 {        
    return $this->user()->can('view categories');
 }

}
bishoy wagih
  • 41
  • 1
  • 4
  • 2
    Smells like redirection. Can you share the whole `$response` including the headers, too. – vahdet Jun 17 '18 at 10:55
  • Please check my updates.. – bishoy wagih Jun 17 '18 at 11:22
  • I don't think it is an error, it is just that your test is broken (unstable). Rewrite the code until the application matches the requirement of the test or change the test if requirements changed (here it looks like you need to expect a 302 status code instead of a 200 one if the code is fine). Whether the code you have put under test is fine or not has not been specified in the question so it is hard to give a more clear guidance. I can't see that the test asserts the user is authenticated. – hakre Jun 17 '18 at 19:39

1 Answers1

0

Probably not signed in! looking at signedIn

Harry Bosh
  • 3,611
  • 2
  • 36
  • 34