Summer Sale! All accounts are 50% off this week.

Mick79's avatar

Undefined method 'factory'

When running tests I get this:

1) Tests\Feature\LoginTest::test_user_can_login_with_correct_credentials
Error: Call to undefined function Tests\Feature\factory()

The test looks like this:

public function test_user_can_login_with_correct_credentials()
    {
        $user = factory(User::class)->create([
            'password' => bcrypt($password = 'i-love-laravel'),
        ]);

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

        $response->assertRedirect('/dashboard');
        $this->assertAuthenticatedAs($user);
    }

But this is being flagged with the error:

Undefined function 'factory'

I am using L9

Any ideas?

0 likes
3 replies
Mick79's avatar
Mick79
OP
Best Answer
Level 5

D'oh, as always I figured out minutes after posting this.

For anyone else, it needs changed to this

  $user = User::factory()->create([
            'password' => bcrypt($password = 'i-love-laravel'),
        ]);
Snapey's avatar

one of the things that have changed over time, so calling factory is now a trait on the model rather than a helper method. Most testing tutorials will have the earlier syntax

1 like
Mick79's avatar

@Snapey Cheers for the context. Think my issue is I started this business in L5 and upgraded manually all the way through L9. I obviously missed some stuff on the upgrades!

Please or to participate in this conversation.