【发布时间】:2021-12-11 15:54:30
【问题描述】:
在我的 Laravel 项目中,我有这个测试:
/** @test */
public function tagControllerStore()
{
$this->post(action([TagController::class, 'store']), [
'name' => 'My tag',
'type' => 'My type',
'color' => 'white'
])
->assertOk();
}
这是我的控制器和其中的存储方法:
<?php
namespace Domain\Shared\Http\Controllers;
class TagController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store(StoreTagRequest $request, Tag $tag)
{
$tag = Tag::create($request->validated());
return response()->json([
'id' => $tag->id
]);
}
}
结果是这样的:
InvalidArgumentException: Action Domain\Shared\Http\Controllers\TagController@store not defined.
我该如何解决这个问题?
【问题讨论】:
-
你有
TagController::class上的店铺方法吗 -
是的,我有。
-
将 store 方法的代码与类的命名空间和类添加到您的问题中。
-
@N69S 我做到了,我只是离开了使用行。
-
如果您的控制器位于正确的文件夹
Domain/Shared/Http/Controllers/中,或者您已将该文件夹与 composer.json 文件中的命名空间相关联,则执行composer -dumpautoload并再次尝试您的测试。