deepu07
276
15
Testing

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

Posted 11 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.


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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.