Support the ongoing development of Laravel.io →
Eloquent Testing

I'm trying to create a mock model for my controllers to use, but doesn't seem to be working

<?php

class QuestionsControllerTest extends TestCase {

    protected $questionMock;

    public function setUp()
    {
        $this->questionMock = Mockery::mock('Question');
    }

    public function testQuestionIndex()
    {
        $this->questionMock
           ->shouldReceive('latest')
           ->once();
        
        $this->app->instance('Question', $this->questionMock);

        $this->call('GET', 'questions');
    }
...

Below is my controller:

<?php namespace App\Http\Controllers;

use App\Http\Requests\QuestionRequest;
use App\Http\Controllers\Controller;

use App\Question;

use Auth;
use Request;

class QuestionsController extends Controller {

    protected $question;

    public function __construct(Question $question)
    {
        $this->question = $question;
    }

    public function index()
    {
        $questions = $this->question->latest()->get();
    
        return view('questions.index', compact('questions'));
    }
...

However, I get the following error:

$ ./vendor/bin/phpunit tests/controllers/QuestionsControllerTest.php 
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Configuration read from /var/www/qasystem-admin/phpunit.xml

E

Time: 135 ms, Memory: 12.50Mb

There was 1 error:

1) QuestionsControllerTest::testQuestionIndex
Mockery\Exception\InvalidCountException: Method latest() from Mockery_0__Question should be called
exactly 1 times but called 0 times.

/var/www/qasystem-admin/vendor/mockery/mockery/library/Mockery/CountValidator/Exact.php:36
/var/www/qasystem-admin/vendor/mockery/mockery/library/Mockery/Expectation.php:271
/var/www/qasystem-admin/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:120
/var/www/qasystem-admin/vendor/mockery/mockery/library/Mockery/Container.php:296
/var/www/qasystem-admin/vendor/mockery/mockery/library/Mockery/Container.php:281
/var/www/qasystem-admin/vendor/mockery/mockery/library/Mockery.php:140
/var/www/qasystem-admin/tests/controllers/QuestionsControllerTest.php:15

What am I doing wrong? I'm worried that perhaps Laravel 5 is different, I can't really find much tutorials or documentation for testing on Laravel 5. The L5 docs doesn't really cover mocking models. Can anyone help? Thanks

Last updated 2 years ago.
0

I had the same problem and it was solved by passing full name of class that you want to mock.

With this in mind your mocking code will look like:

$this->questionMock = Mockery::mock('App\Question');
...
$this->app->instance('App\Question', $this->questionMock);
Last updated 10 years ago.
0

To anyone looking at this question in the future: Laravel now recommends ::class syntax in the framework.

$this->questionMock = Mockery::mock(Question::class);
$this->app->instance(Question::class, $this->questionMock);
0

Sign in to participate in this thread!

Eventy

Your banner here too?

martynbiz martynbiz Joined 15 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.