InvalidArgumentException: Unable to locate factory with name [default] [App\Article].

Published 5 months ago by deepu07

I'm trying to test an Article model with laravel-factory. When I hit phpunit I'm getting an error like this

``PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from C:\xampp7\htdocs\UnitTest\phpunit.xml

E.......

Time: 148 ms, Memory: 6.00MB

There was 1 error:

  1. ArticleTest::test_it_fetches_trending_articles InvalidArgumentException: Unable to locate factory with name [default] [App\Article].

C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:248 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:122 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:260 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:193 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:194 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:148 C:\xampp7\htdocs\UnitTest\tests\Unit\ArticleTest.php:15

FAILURES! Tests: 8, Assertions: 7, Errors: 1. ``

ArticleFactory.php#


use Faker\Generator as Faker;

$factory->define(App\Article::class, function (Faker $faker) {
    return [
        'title' => $faker->sentence
    ];
});```

#ArticleTest.php#


```<?php

use App\Article;


class ArticleTest extends PHPUnit_Framework_TestCase
{

    /** @test */
    
    function it_fetches_trending_articles()
    {

         factory(App\Article::class, 3)->create();

         factory(App\Article::class)->create(['read' => 10]);

         $mostpopular = factory(App\Article::class)->create(['read' => 20]);


         $articles = Article::trending();

         $this->AsserEquals($mostpopular->id, $articles->first()->id);
    }

}```

But it is working fine with Tinker. Can anyone help me out. Thanks in advance.


Best Answer (As Selected By deepu07)
tykus

You need to import it

use Tests\TestCase;

class ArticleTest extends TestCase
{
    //
bobbybouwmann

You need to prefix your factory class with an extra \

$factory->define(\App\Article::class, function (Faker $faker) {
    return [

    ];
});
deepu07

@bobbybouwmann I'm still getting same error

bobbybouwmann

Have you tried running composer dump-autoload?

deepu07

I did but no luck @bobbybouwmann

bobbybouwmann

Are you sure the class exists?

deepu07

Yes. I'm sure.

deepu07

#getting like this#

C:\xampp7\htdocs\UnitTest>phpunit

PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from C:\xampp7\htdocs\UnitTest\phpunit.xml

E.......

Time: 172 ms, Memory: 6.00MB

There was 1 error:

  1. ArticleTest::it_fetches_trending_articles InvalidArgumentException: Unable to locate factory with name [default] [App\Article].

C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:248 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:122 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:260 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:193 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:194 C:\xampp7\htdocs\UnitTest\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:148 C:\xampp7\htdocs\UnitTest\tests\Unit\ArticleTest.php:15

FAILURES! Tests: 8, Assertions: 7, Errors: 1. 

bobbybouwmann

Is your factory loaded correctly?

// composer.json

"autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\": "app/"
        }
    },

Now run composer dump-autoload and your custom factory should be loaded!

In older versions there was just one factory file which was not autoloaded like classes. This might be the issue.

deepu07

It looks like same. I'm using Laravel 5.5 Version. and one more why I don't have ModelFactory.php file instead of that I have UserFactory.php now for article factory thing i was created ArticleFactory.php Does it make sense?? Or do I need to change??

Is this right?? class ArticleTest extends PHPUnit_Framework_TestCase

NowAndHere

if you're importing Article class in your test file, don't refer to factory via App\Article class, use just Article instead:

// Test file

use App\Article;

class ArticleTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    function checking_sth()
    {
        // Arrange
        // Create an article
        $article = factory(Article::class)->create();

       (...)
deepu07

use App\Article;

class ArticleTest extends PHPUnit_Framework_TestCase
{

    /** @test */
    
    function it_fetches_trending_articles()
    {

         factory(Article::class, 3)->create();

         factory(Article::class)->create(['read' => 10]);

         $mostpopular = factory(Article::class)->create(['read' => 20]);


         $articles = Article::trending();

         $this->AsserEquals($mostpopular->id, $articles->first()->id);
    }

}```
shez1983

if your UserFactory is inside factories folder then that is all good..

& u dont need to change..

ArticleTest should extend testcase (its a class inside tests folder) which extends PHPUnit_Framework_TestCase

deepu07

When I run with TestCase i.e class ArticleTest extends TestCase I' m getting error like this

``C:\xampp7\htdocs\UnitTest>phpunit

PHP Fatal error: Class 'TestCase' not found in C:\xampp7\htdocs\UnitTest\tests\Unit\ArticleTest.php on line 6

Fatal error: Class 'TestCase' not found in C:\xampp7\htdocs\UnitTest\tests\Unit\ArticleTest.php on line 6``

tykus
tykus
5 months ago (565,390 XP)

You need to import it

use Tests\TestCase;

class ArticleTest extends TestCase
{
    //
deepu07

yayy I got it! Thanks for everyone.

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