Laravel测试api Post方法不允许HttpException
您好,我正在使用Laravel 5.5,我的测试中有Laravel测试api Post方法不允许HttpException,laravel,unit-testing,laravel-5,Laravel,Unit Testing,Laravel 5,您好,我正在使用Laravel 5.5,我的测试中有 public function testCreate() { $userAdmin = factory(User::class)->create(); $roleAdmin = Role::where('name', 'admin')->first(); $userAdmin->roles()->attach($roleAdmin); $thi
public function testCreate()
{
$userAdmin = factory(User::class)->create();
$roleAdmin = Role::where('name', 'admin')->first();
$userAdmin->roles()->attach($roleAdmin);
$this->actingAs($userAdmin)->post('/api/object/create')
->assertJson(["success"=>true]);
}
在我的路线中:
Route::group(['middleware' => 'auth:api'], function () {
Route::group(['prefix'=>'object'], function(){
Route::post('create', 'ObjectController@create')->middleware('can:create,App\Models\Object');
});
});
但当我运行测试返回时:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
我已尝试通过以下方式清除路线:
php artisan route:cache
以及:
$this->actingAs($userAdmin, 'api')->post('/api/object/create')
及
我在中间件API中使用的策略函数是:
public function create(User $user){
return $user->isAdmin();
}
但它不起作用。我错了什么?您需要登录,因为您有身份验证中间件。您需要登录,因为您有身份验证中间件。php artisan route:list的输出是什么?这条路径是在
web.php
还是api.php
中?在api.php中,输出是:POST | api/object/create | App\Http\Controllers\ObjectController@create| api,auth:api,can:create,App\Models\object您说您试图使用php artisan route:cache
清除路由,但要清除路由,您将使用route:clear
。您是否使用了错误的命令,或者这只是问题中的一个错误?能否在问题中包含route:list
的完整输出?我试图排除无意中与请求匹配的另一个路由,而不是预期的路由定义。php artisan route:list的输出是什么?这条路径是在web.php
还是api.php
中?在api.php中,输出是:POST | api/object/create | App\Http\Controllers\ObjectController@create| api,auth:api,can:create,App\Models\object您说您试图使用php artisan route:cache
清除路由,但要清除路由,您将使用route:clear
。您是否使用了错误的命令,或者这只是问题中的一个错误?能否在问题中包含route:list
的完整输出?我试图排除无意中与请求匹配的另一个路由,而不是预期的路由定义。
public function create(User $user){
return $user->isAdmin();
}