title: Mocking an Eloquent collection response from another mocked class
body: <p>I Have looked at many questions along the same line of thought here on stack overflow, and else where but unable to find a solution to this particular issue.</p>
  
  <p>I'm fairly new to Unit Testing in general, so the mistake may be (hopefully) obvious to someone with more experience.</p>
  
  <p>Here's the issue:</p>
  
  <p>I have a ResourceController that injects a class into the constructor using Depedency Injection.</p>
  
  <pre><code>public function __construct(ResourceAPIInterface $api)
  {
  
      $this-&gt;api = $api; 
  
  }
  </code></pre>
  
  <p>When that API is called in the controller, the class that was injected does some business logic and returns an Eloquent Collection.</p>
  
  <pre><code>public function index($resource, $version)
  {
      $input = Input::all();
      //Populate Data
      $data = $this-&gt;api-&gt;fetchAll($input);
  
  
      //Format response
      if($data-&gt;isEmpty()){
          //Format response
          $response = Response::make(" ", 204);
             }else {
          //Format response
          $response = Response::make($data, 200);
             }
  
      //Set content-type in header
      $response-&gt;header('Content-Type', 'application/json');
      $response-&gt;header('Cache-Control', 'max-age=3600');
      return $response;
  }
  </code></pre>
  
  <p>As you can see from the code above, I need the response to be an eloquent response so i can test to see if it's empty. The method FetchAll literally just returns a Eloquent collation of all records in the table. When I do the test, i'm able to mock the API without issue. However when i'm mocking the response, i really want the response to be an eloquent collection, and having difficulty getting that to work. Here's an example of the test:</p>
  
  <pre><code>            $course = Mockery::mock(new API\Entity\v1\Test);
      $this-&gt;mock-&gt;shouldReceive('fetchAll')-&gt;once()-&gt;andReturn($course-&gt;all());
      $this-&gt;mock-&gt;shouldReceive('name')-&gt;once()-&gt;andReturn('Course');
  
      // Act... 
      $response = $this-&gt;action('GET', 'ResourceController@show'); 
  
      // Assert... 
      $this-&gt;assertResponseOk(); 
  </code></pre>
  
  <p>The above works, but when i want to do the same test against the show method and mock the eloquent response for ->first() I'm getting errors.</p>
  
  <pre><code>  1) ResourceControllerTest::testshow
     BadMethodCallException: Method Mockery_1_API_Entity_v1_Test_API_Entity_v1_Test::first() does not exist on this mock object
  </code></pre>
  
  <p>I've tried to test the model by doing:</p>
  
  <pre><code>        $course = Mockery::mock('Eloquent', 'API\Entity\v1\Test');
             $response = $course-&gt;mock-&gt;shouldReceive('find')-&gt;with(1)-&gt;once()-&gt;andReturn((object)array('id'=&gt;1, 'name'=&gt;'Widget-name','description'=&gt;'Widget description'));
  </code></pre>
  
  <p>However when I run that in the Test I get the following error:</p>
  
  <pre><code>  1) ResourceControllerTest::testIndex
    BadMethodCallException: Method Mockery_1_API_Entity_v1_Test::getAttribute() does not exist on this mock object
  </code></pre>
  
  <p>Any Ideas on how to resolve this issue? Also, if there's a better way to test if the eloquent collection is empty that might resolve some of the complexity that I'm running into is also welcome.</p>