testing returning InvalidArgumentException when using factory

Published 3 months ago by nhayder

i was watching jeffries let's build a forum together to learn some testing stuff, but i got stuck with this error.

Time: 142 ms, Memory: 16.00MB

There was 1 error:

1) Tests\Unit\ThreadsTest::a_user_can_browse_threads
InvalidArgumentException: You requested 1 items, but there are only 0 items available.

/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Support/Arr.php:480
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1298
/Applications/MAMP/htdocs/database/factories/ThreadFactory.php:7
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:263
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:281
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:122
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:289
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:208
/Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:167
/Applications/MAMP/htdocs/tests/Unit/ThreadsTest.php:17

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

not sure what is going on but this is my test

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class ThreadsTest extends TestCase
{
    
    use DatabaseMigrations;

    /** @test */
    public function a_user_can_browse_threads()
    {
        $thread = factory('App\Thread')->create();

        $response = $this->get('/threads');
        
        $response->assertSee($thread->title);


    }
} 

the factory part is the reason for the error. Any ideas?

Best Answer (As Selected By nhayder)
tykus

data are generated on all tables in DB

It appears not. You are getting the error because there are no user id's returned by User::all(['id']) - it is empty

Try this:

// ...

    'user_id' => User::count() ? User::pluck('id')->random() : factory(User::class)->create(),

// ...
tykus
tykus
3 months ago (659,440 XP)

What does the Factory look like?

nhayder

@tykus this is the factory

<?php

use Faker\Generator as Faker;

$factory->define(App\Thread::class, function (Faker $faker) {
    return [
        'user_id' => App\User::all(['id'])->random(),
        'title' => $faker->sentence(10),
        'body' => $faker->paragraph($nbSentences = 3, $variableNbSentences = true),
    ];
});

data are generated on all tables in DB

tykus
tykus
3 months ago (659,440 XP)

data are generated on all tables in DB

It appears not. You are getting the error because there are no user id's returned by User::all(['id']) - it is empty

Try this:

// ...

    'user_id' => User::count() ? User::pluck('id')->random() : factory(User::class)->create(),

// ...
nhayder

@tykus so what is your recomendation

tykus
tykus
3 months ago (659,440 XP)

what is your recomendation

See added code block

Please sign in or create an account to participate in this conversation.