[英]I am with problem to run test in project Laravel
My test function is this我的测试 function 是这个
use RefreshDatabase;
public function create_route()
{
$user = \App\Models\User::factory()->create();
$this->actingAs($user)
->post(route('daily-logs.store'), [
'log' => 'Logging from create route test',
'day' => '2020-01-01',
]);
$this->assertDatabaseHas('daily_logs', [
'user_id' => $user->id,
'log' => 'Logging from create route test',
'day' => '2020-01-01 00:00:00',
]);
}
My route is this:我的路线是这样的:
Route::post('/daily-logs', [DailyLogController::class, 'store'])->name('daily-logs.store');
My controller is this:我的 controller 是这样的:
public function store(Request $request)
{
return DailyLog::create([
'user_id'=> $request->user()->id,
'log'=> $request->log,
'day'=> $request->day,
]);
}
if i access my router using the browser with request post, the job was done successfully.如果我使用带有请求发布的浏览器访问我的路由器,则工作已成功完成。 But, if I am using my test function, an error message is displayed.
但是,如果我使用我的测试 function,则会显示一条错误消息。
Whats wrong?怎么了?
It seems the action was never performed.似乎从未执行过该操作。
It is a good practice in Laravel tests to create an assertion on HTTP calls.在 Laravel 测试中创建对 HTTP 调用的断言是一种很好的做法。
eg例如
$this->post(...)
->assertSuccessful(); // or assertRedirect(), assertStatus(), ...
This will only tell you that the status code (eg 302 does not match a successful status code).这只会告诉您状态代码(例如 302 与成功状态代码不匹配)。
To debug, one way is to dump the response:要调试,一种方法是转储响应:
$this->post(...)
->dump();
$fillable = ['log','day','user_id']; $fillable = ['log','day','user_id'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.