简体   繁体   English

我在项目 Laravel 中运行测试时遇到问题

[英]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.

相关问题 我正在寻找一种使用 API 测试工具在 Laravel 项目上测试 API 的方法 - I am looking for a way to test APIs on Laravel project with an API testing tool 未定义属性:运行 laravel 项目的 stdClass::$column_name 问题 - Undefined property: stdClass::$column_name problem for run laravel project 创建 Laravel 项目的问题 - 不要以 root/超级用户身份运行 Composer - Problem with creating laravel project - do not run Composer as root/super user 我在使用laravel将数据插入数据库时​​遇到问题 - I am having a problem to insert data into database using laravel 我要迁移 laravel 中的表但有问题 - i am going to migrate a table in laravel but having a problem 嗨,每当我在 laravel 中运行 Artisan 时,我都会收到错误消息 - Hi I am getting an error whenever i run Artisan in laravel 控制器中的未定义变量我正在尝试在Laravel中使用phpunit进行测试 - Undefined variable in controller I am trying to test with phpunit in laravel 当我尝试laravel 5项目时如何修复403错误 - How to fix 403 error when i am trying laravel 5 project 将我的 Laravel 项目上传到服务器后出现错误 - I am getting error after uploading my Laravel project to server 我在laravel项目中导入bulma但是样式不起作用 - I am importing bulma in laravel project but the styling is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM