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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 4:57









      miken32

      22.9k84671




      22.9k84671






















          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();






          share|improve this answer






















          • 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











          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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();






          share|improve this answer






















          • 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











          • 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














          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();






          share|improve this answer






















          • 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











          • 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












          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();






          share|improve this answer














          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();







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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










          • 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










          • 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










          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)