Laravel 5.0 User Eloquent Model Mockery

Michael Chekin

I have been trying to use Mockery in my Laravel application's Controller tests for some time, but nothing seems to work.

UserController.php:

class UserController extends Controller
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Display a listing of the resource.
     * GET /user
     *
     * @return Response
     */
    public function index()
    {
        $users = $this->user->orderBy('created_at', 'desc')->paginate(6);
        return view('users.index', compact('users'));
    }
}

My Test:

class UserControllerTest extends TestCase {

    protected $user;
    protected $builder;
    protected $paginator;

    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testIndex()
    {
        $this->user = Mockery::mock('App\User');

        $this->user
            ->shouldReceive('orderBy')
            ->once()
        ;

        $this->app->instance('App\User', $this->user);
        $response = $this->call('GET', 'users');

        $this->assertEquals(200, $response->getStatusCode());
    }

}

When I run PHPUnit I get output:

[Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function paginate() on a non-object

When I add ->andReturn($this->user) :

$this->user
        ->shouldReceive('orderBy')
        ->once()
        ->andReturn($this->user)
 ;

I get PHPUnit output of:

Failed asserting that 500 matches expected 200.
Expected :200
Actual   :500

And when I print the response it has the following error inside it:

Method Mockery_0_App_User::paginate() does not exist on this mock object

When I mock the paginate() function it just fails again on 'getIterator' function in the View.

So what am I doing wrong? I watched tutorials, read articles and related answers here, but haven't found a solution yet.

Fredrik Salin

You would have to mock paginate and it has to return an array of users.

Your expectation should look like this:

$this->user
    ->shouldReceive('orderBy->paginate')
    ->once()
    ->andReturn($userArray);

where $userArray is an array with users on the form you would use it in the view. I guess that's why you get the "getIterator" error, probably you return something that can't be iterated properly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel mock with Mockery Eloquent models

From Dev

Laravel 5 Eloquent Model Object in AngularJS Controller

From Dev

Laravel 5 - eloquent model - 3 tables in series

From Dev

Laravel 5 Eloquent Model Object in AngularJS Controller

From Dev

laravel 4 mockery mock model relationships

From Dev

laravel 4 mockery mock model relationships

From Dev

Laravel 5 User Model not found

From Dev

How to join three table by laravel eloquent model Laravel 5

From Dev

Laravel mockery

From Dev

Laravel 5 eloquent model won't let me update

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

Laravel 5 eloquent load model properties after create

From Dev

Laravel 5 Model: Do not include certain columns in Eloquent

From Dev

Get Table values using Eloquent Model in Laravel5

From Dev

Laravel 5 Model: Do not include certain columns in Eloquent

From Dev

Model Form with CRUD in Laravel 5 (Edit User)

From Dev

Laravel Eloquent model events

From Dev

Laravel Eloquent Model Attributes

From Dev

Laravel Eloquent Model Unbindings

From Dev

A __construct on an Eloquent Laravel Model

From Dev

Laravel Eloquent Model Attributes

From Dev

Laravel eloquent model relationship

From Dev

Transaction with Eloquent Laravel 5

From Dev

AND statement in laravel 5 eloquent

From Dev

Laravel Eloquent 5 Tables

From Dev

Temporary property for Laravel Eloquent model

From Dev

Laravel Eloquent Model Unit testing

From Dev

Laravel eloquent orm - model inheritance

From Dev

Laravel Eloquent: merge model with Input