Answers.Body: <p>I figured out the problem, I needed to load in the service, and then my service needed to load in the model and record. So for the test code, I added the function:</p>
  
  <pre><code>protected function loadServices()
  {
      require_once __DIR__ . '/../services/TournamentData_IndividualService.php';
  }
  </code></pre>
  
  <p>Then for my setUp function, it became:</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  use \Mockery as m;
  use PHPUnit_Framework_TestCase;
  class TournamentData_IndividualServiceTest extends PHPUnit_Framework_TestCase
  {
      public function setUp()
      {
          $this-&gt;loadServices();
          $this-&gt;ingredientRecord = m::mock('Craft\TournamentData_IndividualRecord');
          $this-&gt;service = new TournamentData_IndividualService($this-&gt;ingredientRecord);
      }
  }
  </code></pre>
  
  <p>Then in my service method, I added require statements for the model and record like so:</p>
  
  <pre><code>&lt;?php
  
  require_once __DIR__ . '/../models/TournamentData_IndividualModel.php';
  require_once __DIR__ . '/../records/TournamentData_IndividualRecord.php';
  
  namespace Craft;
  
  class TournamentData_IndividualService extends BaseApplicationComponent
  {
      protected $individualRecord;
  
      public function __construct($individualRecord = null)
      {
          $this-&gt;individualRecord = $individualRecord;
          if(is_null($this-&gt;individualRecord)) {
              $this-&gt;individualRecord = TournamentData_IndividualRecord::model();
          }
      }
  }
  </code></pre>
  
  <p>Ironically, I managed to find the answer to my problem almost immediately after deciding to ask the community, but hopefully this helps somebody else in the future!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-03T22:50:21.723Z
Answers.LastActivityDate: 2017-04-03T22:50:21.723Z
Answers.UserDisplayName: Chris
Body: <p>I am writing a plugin and want to unit test my services.  I am following the example given here: <a href="https://github.com/amacneil/cocktailrecipes" rel="nofollow noreferrer">https://github.com/amacneil/cocktailrecipes</a>. The problem is when I run my application, I get a</p>
  
  <pre><code>PHP Fatal Error: Class 'TournamentData_IndividualService' not found.
  </code></pre>
  
  <p>Here is the problem portion of the test code:</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  use \Mockery as m;
  use PHPUnit_Framework_TestCase;
  
  class TournamentData_IndividualServiceTest extends PHPUnit_Framework_TestCase
  {
      public function setUp()
      {
          $this-&gt;ingredientRecord = m::mock('Craft\TournamentData_IndividualRecord');
          $this-&gt;service = new TournamentData_IndividualService($this-&gt;ingredientRecord);
      }
  }
  </code></pre>
  
  <p>Here is my service code:</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class TournamentData_IndividualService extends BaseApplicationComponent
  {
      protected $individualRecord;
  
      public function __construct($individualRecord = null)
      {
          $this-&gt;individualRecord = $individualRecord;
          if(is_null($this-&gt;individualRecord)) {
              $this-&gt;individualRecord = TournamentData_IndividualRecord::model();
          }
      }
  }
  </code></pre>
  
  <p>One more note, just in case this has the answer to what I'm looking for, I have taken a look at <a href="https://github.com/selvinortiz/craft.loath" rel="nofollow noreferrer">https://github.com/selvinortiz/craft.loath</a>, but I wasn't able to follow what its setup code was accomplishing.</p>
  
  <p>Any help is greatly appreciated!</p>
  
Tags: <plugin-development>
Title: Class not found error when running unit test on a plugin service
ThreadText: # Class not found error when running unit test on a plugin service
  
  (Asked by: username_0 on 2017-04-03)
  
  I am writing a plugin and want to unit test my services. I am following the example given here: <https://github.com/amacneil/cocktailrecipes>. The problem is when I run my application, I get a
  
  ```
  PHP Fatal Error: Class 'TournamentData_IndividualService' not found.
  
  ```
  
  Here is the problem portion of the test code:
  
  ```
  <?php
  namespace Craft;
  
  use \Mockery as m;
  use PHPUnit_Framework_TestCase;
  
  class TournamentData_IndividualServiceTest extends PHPUnit_Framework_TestCase
  {
      public function setUp()
      {
          $this->ingredientRecord = m::mock('Craft\TournamentData_IndividualRecord');
          $this->service = new TournamentData_IndividualService($this->ingredientRecord);
      }
  }
  
  ```
  
  Here is my service code:
  
  ```
  <?php
  namespace Craft;
  
  class TournamentData_IndividualService extends BaseApplicationComponent
  {
      protected $individualRecord;
  
      public function __construct($individualRecord = null)
      {
          $this->individualRecord = $individualRecord;
          if(is_null($this->individualRecord)) {
              $this->individualRecord = TournamentData_IndividualRecord::model();
          }
      }
  }
  
  ```
  
  One more note, just in case this has the answer to what I'm looking for, I have taken a look at <https://github.com/selvinortiz/craft.loath>, but I wasn't able to follow what its setup code was accomplishing.
  
  Any help is greatly appreciated!
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-04-03. Score: 2
  
  I figured out the problem, I needed to load in the service, and then my service needed to load in the model and record. So for the test code, I added the function:
  
  ```
  protected function loadServices()
  {
      require_once __DIR__ . '/../services/TournamentData_IndividualService.php';
  }
  
  ```
  
  Then for my setUp function, it became:
  
  ```
  <?php
  namespace Craft;
  
  use \Mockery as m;
  use PHPUnit_Framework_TestCase;
  class TournamentData_IndividualServiceTest extends PHPUnit_Framework_TestCase
  {
      public function setUp()
      {
          $this->loadServices();
          $this->ingredientRecord = m::mock('Craft\TournamentData_IndividualRecord');
          $this->service = new TournamentData_IndividualService($this->ingredientRecord);
      }
  }
  
  ```
  
  Then in my service method, I added require statements for the model and record like so:
  
  ```
  <?php
  
  require_once __DIR__ . '/../models/TournamentData_IndividualModel.php';
  require_once __DIR__ . '/../records/TournamentData_IndividualRecord.php';
  
  namespace Craft;
  
  class TournamentData_IndividualService extends BaseApplicationComponent
  {
      protected $individualRecord;
  
      public function __construct($individualRecord = null)
      {
          $this->individualRecord = $individualRecord;
          if(is_null($this->individualRecord)) {
              $this->individualRecord = TournamentData_IndividualRecord::model();
          }
      }
  }
  
  ```
  
  Ironically, I managed to find the answer to my problem almost immediately after deciding to ask the community, but hopefully this helps somebody else in the future!
  
  
  ---
  
  