mardi 7 avril 2015

Mocking Repository Interface in Laravel 5

I am struggling to mock my Interface in my tests, I keep getting



UserInterface should be called exactly 1 times but called 0 times.


Example of my interface:



<?php namespace App\Contracts\User;

interface UserInterface {

public function getAllUsers();

}


Example of my repository:



<?php namespace App\Contracts\User;

use App\User;

class UserRepository implements UserInterface {

protected $user;

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

public function getAllUsers()
{
return $this->user->all();
}


And my test



<?php namespace Tests\Controllers;

use TestCase;
use Mockery;

class UserIControllerTest extends TestCase {

public function setUp()
{
parent::setup();
$this->user = Mockery::mock('App\Contracts\User\UserInterface');
}

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

public function testgetAllUserscall()
{
$mock = $this->user->shouldReceive('getAllUsers')->once()->andReturn('foo');
$this->app->instance('App\Contracts\User\UserInterface', $mock);

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

}


When I go to the route manually I get the desired result, but the mocking goes wrong. What am I missing ?


Aucun commentaire:

Enregistrer un commentaire