Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to locate factory with name [default] #28378

Closed
mesadhan opened this issue May 1, 2019 · 12 comments
Closed

Unable to locate factory with name [default] #28378

mesadhan opened this issue May 1, 2019 · 12 comments

Comments

@mesadhan
Copy link

mesadhan commented May 1, 2019

  • Laravel Version: 5.8.*
  • PHP Version: 7.2.5
  • Database Driver & Version: sqlite

Description:

public function testCreateRecord()
{
	// case 1:
	$factory = factory('App\User')->create([
		'name' => 'test',
		'email' => 'test@laravel.com',
		'password' => Hash::make('1'),
	]);

	// case 2:
	$factory = factory('App\Http\Models\Stock')->create([
		'name' => 'mango',
		'price' => 300,
	]);

	$this->assertEquals('mango', $stock->name);
	$this->assertEquals(300, $stock->name);
}

I don't know case 1 working fine but why case 2 throws an error.
(InvalidArgumentException : Unable to locate factory with name [default] [App\Http\Models\Stock].)

As I defined an App\Http\Models\Stock model and it's working properly, But not working in factory method.

Steps To Reproduce:

  • Create new laravel project.
  • Create Model base on the given namespace
  • Then Try to use factory method
@brunogaspar
Copy link
Contributor

Ensure your Stock model has the correct namespace, otherwise the error you're receiving is normal

@mesadhan
Copy link
Author

mesadhan commented May 1, 2019

It's working fine. namespace given properly. when I execute

 $stock = Stock::create([
        'name' => 'mango',
       'price' => 200,
]);

It's working fine. and store data in database.

@brunogaspar
Copy link
Contributor

brunogaspar commented May 1, 2019

Can you paste your Stock model code?

@mesadhan
Copy link
Author

mesadhan commented May 1, 2019


<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class Stock extends Model
{
    //
    protected $fillable = [
        'name',
        'price'
    ];

    public $timestamps = true;
}


@mesadhan mesadhan closed this as completed May 1, 2019
@timur-gilauri
Copy link

Hey, how have you solved it???

@felloz
Copy link

felloz commented Dec 16, 2019

Same problem here!

@browner12
Copy link
Contributor

this most likely looks like a userland error, not a framework bug.

the problem is most likely in your StockFactory class.

please move this conversation over to:

Laravel Slack (https://larachat.co/)
Laravel.io Forum (https://laravel.io/forum)
Laracasts Forum (https://laracasts.com/discuss)
StackOverflow (http://stackoverflow.com/questions/tagged/laravel)

Thanks!

@timur-gilauri
Copy link

I had this issue during develop artisan console command trying to pass model class full name with namespace as command option. So I found out that the class path must start with back slash \ like "\App\Models\Model". Even if you see factories defined without it.

@wdarking
Copy link

In my case it was because unit tests stubs in late laravel versions are extending PHPUnit\Framework\TestCase instead of the Tests\TestCase. In this case, the factories will not work because the framework will not be booted. More details here: #30879 (comment)

The solution for me was to make my test extend the laravel TestCase class instead of phpunit TestCase.

Something like this:

<?php

namespace Tests\Unit;

use Tests\TestCase;
// use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class PaymentModelTest extends TestCase
{
    use RefreshDatabase;

    public function testPaymentModelUuidOnCreated()
    {
        $payment = factory(\App\Payment::class)->create();

        $this->assertNotNull($payment->uuid);
    }
}

@mitesh1409
Copy link

@wdarking

In my case it was because unit tests stubs in late laravel versions are extending PHPUnit\Framework\TestCase instead of the Tests\TestCase. In this case, the factories will not work because the framework will not be booted. More details here: #30879 (comment)

The solution for me was to make my test extend the laravel TestCase class instead of phpunit TestCase.

Something like this:

<?php

namespace Tests\Unit;

use Tests\TestCase;
// use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class PaymentModelTest extends TestCase
{
    use RefreshDatabase;

    public function testPaymentModelUuidOnCreated()
    {
        $payment = factory(\App\Payment::class)->create();

        $this->assertNotNull($payment->uuid);
    }
}

I faced exactly the similar issue.
And resolved it by removing
use PHPUnit\Framework\TestCase;
and then adding
use Tests\TestCase;

But is there anyway we can solve it by keeping
use PHPUnit\Framework\TestCase;

It seems that we won't be able to make use of Laravel Model Factories in the Unit tests out of the box.

@wdarking
Copy link

wdarking commented Feb 20, 2020

@mitesh1409 the class Tests\TestCase actually extends the abstract class Illuminate\Foundation\Testing\TestCase, which extends the PHPUnit\Framework\TestCase class.

So, if you want to use the framework features like factories on your Unit Tests, you should use Tests\TestCase.

It seems that we won't be able to make use of Laravel Model Factories in the Unit tests out of the box.

About that: as you can see here they changed the Unit tests TestCase so the tests can run faster, and if you need to use factories on your tests, maybe you should consider placing them in the Feature directory.

But is there anyway we can solve it by keeping use PHPUnit\Framework\TestCase;

Well i think you could create your own base TestCase class which extends PHPUnit\Framework\TestCase and try to load whatever framework features you need (like factories), but i think this would be cumbersome.

@mitesh1409
Copy link

@wdarking
Thanks for your detailed answer.

So I believe if we want to make use of Model Factory feature in unit tests, then importing
"Tests\TestCase" instead of "PHPUnit\Framework\TestCase" is perfectly fine and is not a bad practice.
The only drawback is that it will run slow since it will boot the framework.

Thanks again :)

HonkingGoose added a commit to HonkingGoose/molveno_video_app that referenced this issue Mar 2, 2020
- This ensures a factory can be used with tests.
  PHPUnit doesn't understand/use factories.
  See: laravel/framework#28378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants