【发布时间】:2013-08-04 17:52:36
【问题描述】:
我需要用 Mockery 模拟 Laravel 的 Eloquent\Model,这有点棘手,因为它使用静态方法。
我用下面的代码解决了这个问题,但我想知道是否有更好/更智能的方法来做到这一点。
<?php
use Ekrembk\Repositories\EloquentPostRepository;
class EloquentPostRepositoryTest extends TestCase {
public function __construct()
{
$this->mockEloquent = Mockery::mock('alias:Ekrembk\Post');
}
public function tearDown()
{
Mockery::close();
}
public function testTumuMethoduEloquenttenAldigiCollectioniDonduruyor()
{
$eloquentReturn = 'fake return';
$this->mockEloquent->shouldReceive('all')
->once()
->andReturn($eloquentDongu);
$repo = new EloquentPostRepository($this->mockEloquent);
$allPosts = $repo->all();
$this->assertEquals($eloquentReturn, $allPosts);
}
}
【问题讨论】:
标签: phpunit laravel eloquent mockery