jeudi 25 décembre 2014

Laravel 4 Mocking Issue : should be called exactly 1 times but called 0 times

I have a problem on unit testing my project. This is my code:



namespace Way\Storage\HalisahaAccount;
# app/lib/Way/Storage/HalisahaAccount/HalisahaAccountRepositoryInterface.php

interface HalisahaAccountRepositoryInterface {

public function all();

public function find($id);

public function create($input);

}


And the Eloquent repository



namespace Way\Storage\HalisahaAccount;

# app/lib/Way/Storage/HalisahaAccount/EloquentHalisahaAccountRepository.php

use HalisahaAccount;

class EloquentHalisahaAccountRepository implements HalisahaAccountRepositoryInterface {

public function all()
{
return HalisahaAccount::all();
}

public function find($id)
{
return HalisahaAccount::find($id);
}

public function create($input)
{
return HalisahaAccount::create($input);
}

}


And this is my model: HalisahaAccount



class HalisahaAccount extends Eloquent {

/**
* shouldReceive for test
*/
public static function shouldReceive()
{
$class = get_called_class();
$repo = "Way\\Storage\\{$class}\\{$class}RepositoryInterface";
$mock = Mockery::mock($repo);

App::instance($repo, $mock);

return call_user_func_array([$mock, 'shouldReceive'], func_get_args());
}
}


Controller



class HalisahalarController extends BaseController {


protected $halisahaAccount;


public function __construct (HalisahaAccount $halisahaAccount) {
$this->halisahaAccount = $halisahaAccount;
}

public function getIndex(){
$halisahalar = $this->halisahaAccount->all();
return View::make('index',array('halisahalar' => $halisahalar));
}
}


And my test



class HalisahalarControllerTest extends TestCase {

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

/**
* halısahaların listelendiği sayfanın testi
*/
public function testGetIndex(){

HalisahaAccount::shouldReceive('all')->once();

$this->client->request('GET', '/halisahalar');

$this->assertViewHas('halisahalar');
}
}


I am running the phpunit test, but getting the this error:



1) HalisahalarControllerTest::testGetIndex
Mockery\Exception\InvalidCountException: Method all() from Mockery_0_Way_Storage_HalisahaAccount_HalisahaAccountRepositoryInterface should be called
exactly 1 times but called 0 times.


Why the mockery dont call my all() method?


Aucun commentaire:

Enregistrer un commentaire