Laravel 4 Mocking Issue:应该被称为1次,但被称为0次

时间:2014-12-25 09:06:19

标签: unit-testing laravel-4 phpunit mockery

我的单元测试我的项目有问题。 这是我的代码:

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

interface HalisahaAccountRepositoryInterface {

    public function all();

    public function find($id);

    public function create($input);

}

和Eloquent存储库

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);
  }

}

这是我的模特: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());
    }
}

控制器

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));
    }
}

我的测试

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');
    }
}

我正在运行phpunit测试,但收到此错误:

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.

为什么嘲弄者不会调用我的all()方法?

0 个答案:

没有答案