Rafael Furtado
Rafael Furtado

Reputation: 99

I am with problem to run test in project Laravel

My test function is this

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:

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.

enter image description here

Whats wrong?

Upvotes: 0

Views: 158

Answers (2)

Osmair Coelho
Osmair Coelho

Reputation: 31

You need to change your migration to look for dateTime rather than date.

Schema::create('daily_logs', function (Blueprint $table) {
  $table->id();
  $table->integer('user_id')->unsigned()->index();
  $table->text('log');
  $table->dateTime('day'); // <<<< change date to datetime
  $table->timestamps();
});

Upvotes: 0

tanerkay
tanerkay

Reputation: 3930

It seems the action was never performed.

It is a good practice in Laravel tests to create an assertion on HTTP calls.

e.g.

    $this->post(...)
        ->assertSuccessful(); // or assertRedirect(), assertStatus(), ...

This will only tell you that the status code (e.g. 302 does not match a successful status code).

To debug, one way is to dump the response:

    $this->post(...)
        ->dump();

Upvotes: 1

Related Questions