3

I'm trying to add a Factory to help increment my test coverage on some controllers. I've been using Factories with no issues till now, where I find no reason for this message and I can't figure out what is different from the rest of the factories, which are working perfectly.

The error is:

1) GuestBusinessControllerTest::it_presents_a_domain_home
InvalidArgumentException: Unable to locate factory with name [default] [App\Models\Domain].

I reference the useful files as follows:

My Controller Test trying to use the Factory (Through a trait)

<?php

use Illuminate\Foundation\Testing\DatabaseTransactions;

class GuestBusinessControllerTest extends TestCase
{
    use DatabaseTransactions;
    use CreateBusiness, CreateDomain;

    // ...

    /** @test */
    public function it_presents_a_domain_home()
    {
        $domain = $this->createDomain();

        // ...
    }
}

The trait using the factory

<?php

use App\Models\Domain;

trait CreateDomain
{
    private function createDomain($overrides = [])
    {
        return factory(Domain::class)->create($overrides);
    }

    // ...

}

The factory definition

// ...

$factory('App\Models\Domain', function ($faker) {
    return [
        'slug'     => str_slug($faker->sentence(3)),
        'owner_id' => 'factory:App\Models\User',
    ];
});

// ...

I'm using "laracasts/testdummy": "~2.0"

// ...

    "require-dev": {
        // ...
        "laracasts/testdummy": "~2.0",
        // ...
    },

// ...

Sidenotes:

Yes, I did composer dump-autoload (Else, the error message would be different)

I tried to define the factory in another helper file, and dump-autoload. (Just in case)

I also tried renaming my model, thinking that Domain might be a conflicting keyword, but that seems not to be the issue.

How may I solve this error?

Community
  • 1
  • 1
alariva
  • 2,051
  • 1
  • 22
  • 37
  • 1
    How about defining your factories as advertised? See https://laravel.com/docs/5.1/testing#model-factories. – localheinz Jan 03 '16 at 23:15
  • Good hint, but still, [just tried](http://laravel.io/bin/l5mPV) but the error persists the same. – alariva Jan 04 '16 at 20:09
  • Also tried removing [laracasts/testdummy](https://github.com/laracasts/TestDummy) as factories and faker are included in L5.1, but still same error. – alariva Jan 04 '16 at 20:41

1 Answers1

4

Found the problem

I was all the time thinking I was working with the file tests/factories/factories.php since I was using laracasts/testdummy.

It turns out (probably since the migration to L5.1, but not sure), I was now using database/factories/ModelFactory.php which I one day updated with my old factories, but never removed the tests/factories/factories.php and thus, editing it for new changes was worthless.

Now I've removed this file and kept a single factory file sticking to Laravel 5.1 solution out of the box.

alariva
  • 2,051
  • 1
  • 22
  • 37