Factory helper not working within PHPUnit setup method
up vote
0
down vote
favorite
Writing some unit tests, and I want to have an object created before the tests in the class are done. So I set up the setUpBeforeClass()
method:
<?php
namespace TestsUnit;
use TestsTestCase;
use AppLocation;
class UserTests extends TestCase
const FAKEID = 9999999;
public static function setUpBeforeClass() : void
parent::setUpBeforeClass();
factory(Location::class)->make(["id" => self::FAKEID])->save();
But when I try running this, I get this error:
InvalidArgumentException: Unable to locate factory with name [default] [AppLocation].
But the factory class is set up properly. In fact, if I move this same line down to one of my test functions it works perfectly.
public function testCreateUser()
factory(Location::class)->make(["id" => self::FAKEID])->save();
// do other stuff...
The only thing that sticks out to me as different about setUpBeforeClass()
is that it's a static method, but I don't know why that would prevent the factory class from working.
php laravel phpunit
add a comment |
up vote
0
down vote
favorite
Writing some unit tests, and I want to have an object created before the tests in the class are done. So I set up the setUpBeforeClass()
method:
<?php
namespace TestsUnit;
use TestsTestCase;
use AppLocation;
class UserTests extends TestCase
const FAKEID = 9999999;
public static function setUpBeforeClass() : void
parent::setUpBeforeClass();
factory(Location::class)->make(["id" => self::FAKEID])->save();
But when I try running this, I get this error:
InvalidArgumentException: Unable to locate factory with name [default] [AppLocation].
But the factory class is set up properly. In fact, if I move this same line down to one of my test functions it works perfectly.
public function testCreateUser()
factory(Location::class)->make(["id" => self::FAKEID])->save();
// do other stuff...
The only thing that sticks out to me as different about setUpBeforeClass()
is that it's a static method, but I don't know why that would prevent the factory class from working.
php laravel phpunit
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Writing some unit tests, and I want to have an object created before the tests in the class are done. So I set up the setUpBeforeClass()
method:
<?php
namespace TestsUnit;
use TestsTestCase;
use AppLocation;
class UserTests extends TestCase
const FAKEID = 9999999;
public static function setUpBeforeClass() : void
parent::setUpBeforeClass();
factory(Location::class)->make(["id" => self::FAKEID])->save();
But when I try running this, I get this error:
InvalidArgumentException: Unable to locate factory with name [default] [AppLocation].
But the factory class is set up properly. In fact, if I move this same line down to one of my test functions it works perfectly.
public function testCreateUser()
factory(Location::class)->make(["id" => self::FAKEID])->save();
// do other stuff...
The only thing that sticks out to me as different about setUpBeforeClass()
is that it's a static method, but I don't know why that would prevent the factory class from working.
php laravel phpunit
Writing some unit tests, and I want to have an object created before the tests in the class are done. So I set up the setUpBeforeClass()
method:
<?php
namespace TestsUnit;
use TestsTestCase;
use AppLocation;
class UserTests extends TestCase
const FAKEID = 9999999;
public static function setUpBeforeClass() : void
parent::setUpBeforeClass();
factory(Location::class)->make(["id" => self::FAKEID])->save();
But when I try running this, I get this error:
InvalidArgumentException: Unable to locate factory with name [default] [AppLocation].
But the factory class is set up properly. In fact, if I move this same line down to one of my test functions it works perfectly.
public function testCreateUser()
factory(Location::class)->make(["id" => self::FAKEID])->save();
// do other stuff...
The only thing that sticks out to me as different about setUpBeforeClass()
is that it's a static method, but I don't know why that would prevent the factory class from working.
php laravel phpunit
php laravel phpunit
asked Nov 9 at 4:57
miken32
22.9k84671
22.9k84671
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Laravel does a lot of setting up in the setUp()
method in the TestCase
class. The setUpBeforeClass()
method is called before that, that's why your factory is not loaded yet.
The Laravel's TestCase
class setup
method (see class):
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
if (! $this->app)
$this->refreshApplication();
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback)
call_user_func($callback);
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
Change your code to use setUp
instead:
protected static function setUp() : void
parent::setUp();
factory( Location::class )->make( ["id" => self::FAKEID] )->save();
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCasesetUp
fixture is called..
– Sven van Zoelen
Nov 9 at 14:05
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Laravel does a lot of setting up in the setUp()
method in the TestCase
class. The setUpBeforeClass()
method is called before that, that's why your factory is not loaded yet.
The Laravel's TestCase
class setup
method (see class):
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
if (! $this->app)
$this->refreshApplication();
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback)
call_user_func($callback);
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
Change your code to use setUp
instead:
protected static function setUp() : void
parent::setUp();
factory( Location::class )->make( ["id" => self::FAKEID] )->save();
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCasesetUp
fixture is called..
– Sven van Zoelen
Nov 9 at 14:05
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
add a comment |
up vote
0
down vote
accepted
Laravel does a lot of setting up in the setUp()
method in the TestCase
class. The setUpBeforeClass()
method is called before that, that's why your factory is not loaded yet.
The Laravel's TestCase
class setup
method (see class):
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
if (! $this->app)
$this->refreshApplication();
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback)
call_user_func($callback);
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
Change your code to use setUp
instead:
protected static function setUp() : void
parent::setUp();
factory( Location::class )->make( ["id" => self::FAKEID] )->save();
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCasesetUp
fixture is called..
– Sven van Zoelen
Nov 9 at 14:05
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Laravel does a lot of setting up in the setUp()
method in the TestCase
class. The setUpBeforeClass()
method is called before that, that's why your factory is not loaded yet.
The Laravel's TestCase
class setup
method (see class):
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
if (! $this->app)
$this->refreshApplication();
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback)
call_user_func($callback);
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
Change your code to use setUp
instead:
protected static function setUp() : void
parent::setUp();
factory( Location::class )->make( ["id" => self::FAKEID] )->save();
Laravel does a lot of setting up in the setUp()
method in the TestCase
class. The setUpBeforeClass()
method is called before that, that's why your factory is not loaded yet.
The Laravel's TestCase
class setup
method (see class):
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
if (! $this->app)
$this->refreshApplication();
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback)
call_user_func($callback);
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
Change your code to use setUp
instead:
protected static function setUp() : void
parent::setUp();
factory( Location::class )->make( ["id" => self::FAKEID] )->save();
edited Nov 9 at 13:07
answered Nov 9 at 13:02
Sven van Zoelen
4,18532439
4,18532439
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCasesetUp
fixture is called..
– Sven van Zoelen
Nov 9 at 14:05
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
add a comment |
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCasesetUp
fixture is called..
– Sven van Zoelen
Nov 9 at 14:05
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
But this method is run before every test. Seems a waste to have to create and insert new records before every test, and remove them after every test.
– miken32
Nov 9 at 13:37
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCase
setUp
fixture is called..– Sven van Zoelen
Nov 9 at 14:05
I understand what you're saying, but that's what you get when marrying a framework.. Laravel is already doing it before every test.. You want to use factories based on Laravel, so you need Laravel when it's setup and running, and that's after the TestCase
setUp
fixture is called..– Sven van Zoelen
Nov 9 at 14:05
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Btw, you always should reset the state at the start of every test. Basing tests on previous test states is a no no in unit testing. That's why Laravel also resets itself every test.
– Sven van Zoelen
Nov 9 at 14:13
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
Well what I’m doing is just inserting some records that are needed by foreign keys on the records that I’m actually testing. Like in the example I used I need a location set up before I can create a user. I expect I’ll go with this answer, just seems like unnecessary DB access, over dozens of tests.
– miken32
Nov 9 at 14:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220094%2ffactory-helper-not-working-within-phpunit-setup-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown