0

Hi im trying to implement a test_user_can_login_with_correct_credentials test and i cant figure out why there is a 'factory' undefined error.

I've searched online and people seem to be getting this because they haven't imported the user model but i have

here is the code

<?php

namespace Tests\Feature;



use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;


class LoginTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_user_can_view_a_login_form()
    {
        $response = $this->get('/login');

        $response->assertSuccessful();
        $response->assertViewIs('auth.login');
    }

    public function test_user_can_login_with_correct_credentials()
    {
       $user = factory(User::class)->create([ // Error with 'factory' on this line
           'first_name' =>('John'),
           'last_name' => ('Smith'),
           'email' => ('john@example.com'),
           'date_of_birth' => ('16/02/2001'),
           'password' => Hash::make('test'),
           'join_date' => Carbon::now()
       ]);

       $response = $this->from('/login')->post('/login', [
           'email' => $user->email,
           'password' => 'test',

       ]);
        $response->assertRedirect('/login');
        $response->assertSessionHasErrors('email');
        $this->assertTrue(session()->hasOldInput('email'));
        $this->assertFalse(session()->hasOldInput('password'));
        $this->assertGuest();
    }
}

The exact error is 'Undefined function 'Tests\Feature\factory'.intelephense(1010)'

Any help would be appreciated im unsure of how to resolve this

  • Does this answer your question? [Laravel, Call to undefined function Database\Seeders\factory()](https://stackoverflow.com/questions/63816395/laravel-call-to-undefined-function-database-seeders-factory) – miken32 Mar 11 '21 at 23:03
  • Please read up on how to write factories in Laravel 8, the `factory()` helper function is no longer valid. – miken32 Mar 11 '21 at 23:03

0 Answers0