【问题标题】:Expected status code 200 but received 302预期状态码 200 但收到 302
【发布时间】:2018-11-26 11:59:55
【问题描述】:

我正在尝试执行测试以检查是否

1 - 用户已通过身份验证

2 - 有权查看请求的页面

每次我运行测试时都会收到此错误

预期状态码 200,但收到 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());
  }
}

我的控制器

<?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'));
}
}     

这是我的请求授权请求

<?php

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

}

【问题讨论】:

  • 闻起来像重定向。你能分享整个$response,包括标题。
  • 请检查我的更新..
  • 我不认为这是一个错误,只是你的测试被破坏了(不稳定)。重写代码,直到应用程序与测试要求相匹配,或者如果要求发生变化则更改测试(如果代码正常,您需要期待 302 状态代码而不是 200 状态代码)。您测试的代码是否良好尚未在问题中指定,因此很难给出更清晰的指导。我看不到测试断言用户已通过身份验证。

标签: laravel phpunit


【解决方案1】:

可能没有登录!看着signedIn

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2019-03-20
    • 2020-08-07
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多