Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mockery->shouldReceive() passing when it shouldnt?

I am learning unit testing in laravel using phpunit and mockery. I am currently trying to test UsersController::store().

I am mocking User Model and using it to test the index method and that seems to work. When I take out $this->user->all() the test fails and when its in it passes.

When testing the store method though I am using the mock to test that the user model receives validate() once. The store method is empty but the test passes. I have left out the irrelevant pieces of the class for brevities sake

<?php

class UsersController extends BaseController {

    public function __construct(User $user)
    {
        $this->user = $user;
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $users = $this->user->all();

        return View::make('users.index')
        ->with('users', $users);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }

}

UserControllerTest.php

<?php
    use Mockery as m;
class UserControllerTest extends TestCase {

    public function __construct()
    {
        $this->mock = m::mock('BaseModel', 'User');
    }

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

    public function testIndex()
    {
        $this->mock
            ->shouldReceive('all')
            ->once()
            ->andReturn('All Users');
        $this->app->instance('User', $this->mock);
        $this->call('GET', 'users');
        $this->assertViewHas('users', 'All Users');
    }

    public function testCreate()
    {
        View::shouldReceive('make')->once();
        $this->call('GET', 'users/create');
        $this->assertResponseOk();
    }

    public function testStore()
    {

        $this->mock
            ->shouldReceive('validate')
            ->once()
            ->andReturn(m::mock(['passes' => 'true']));
        $this->app->instance('User', $this->mock);
        $this->call('POST', 'users');
    }


}
like image 883
Mark Avatar asked Dec 11 '13 22:12

Mark


2 Answers

Mockery is by default a stubbing library, not a mocking one (which is confusing because of its name).

That means that ->shouldReceive(...) by default is "zero or more times". When using ->once(), you say it should be called zero or one time, but not more. This means it'll always pass.

When you want to assert that it is called once, you can use ->atLeast()->times(1) (one or more times) or ->times(1) (exactly one time)

like image 68
Wouter J Avatar answered Sep 24 '22 23:09

Wouter J


To complete Wounter's answer, you must call Mockery::close().

This static call cleans up the Mockery container used by the current test, and run any verification tasks needed for your expectations.

This answer helped me understand this concept.

like image 37
Guillermo Mansilla Avatar answered Sep 23 '22 23:09

Guillermo Mansilla