【发布时间】:2015-06-15 22:24:47
【问题描述】:
我正在为客户开发一个应用程序,但在测试存储库时遇到问题。
要将存储库绑定到模型,我有以下代码:
<?php
namespace FD\Repo;
use App;
use Config;
/**
* Service Provider for Repository
*/
class RepoServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register()
{
$app = $this->app;
$app->bind('FD\Repo\FactureSst\FactureSstInterface', function ($app) {
return new FactureSst\EloquentFactureSst(App::make('FactureSst'), new \FD\Service\Cache\LaravelCache($app['cache'], 'factures_sst', 10));
});
}
}
然后,存储库扩展了一个抽象类,其中包含来自 eloquent ORM 的函数(查找、位置、所有等)。存储库的代码如下所示:
<?php
namespace FD\Repo\FactureSst;
use Illuminate\Database\Eloquent\Model;
use FD\Repo\AbstractBaseRepo;
use FD\Repo\BaseRepositoryInterface;
use FD\Service\Cache\CacheInterface;
use Illuminate\Support\Collection;
class EloquentFactureSst extends AbstractBaseRepo implements BaseRepositoryInterface, FactureSstInterface
{
protected $model;
protected $cache;
public function __construct(Model $resource, CacheInterface $cache)
{
$this->model = $resource;
$this->cache = $cache;
}
/**
* Retrieve factures with the given SST and BDC IDs.
*
* @param int $sst_id
* @param int $bdc_id
* @return \Illuminate\Support\Collection
*/
public function findWithSstAndBdc($sst_id, $bdc_id)
{
$return = new Collection;
$factures = $this->model->where('id_sst', $sst_id)
->whereHas('facture_assoc', function ($query) use ($bdc_id) {
$query->where('id_bdc', $bdc_id);
})
->get();
$factures->each(function ($facture) use (&$return) {
$data = [
'facture_id' => $facture->id,
'facture_name' => $facture->num_facture,
'total_dsp' => $facture->total_dsp(),
'total_tradi' => $facture->total_tradi()
];
$return->push($data);
});
return $return;
}
}
为了测试对数据库的调用,我使用了 Mockery,因为对数据库的调用会太长。这是我的测试类:
<?php namespace App\Tests\Unit\Api\FactureSst;
use App;
use FactureSst;
use Illuminate\Database\Eloquent\Collection;
use Mockery as m;
use App\Tests\FdTestCase;
class FactureSstTest extends FdTestCase
{
/**
* The primary repository to test.
*/
protected $repo;
/**
* Mocked version of the primary repo.
*/
protected $mock;
public function setUp()
{
parent::setUp();
$this->repo = App::make('FD\Repo\FactureSst\FactureSstInterface');
$this->mock = $this->mock('FD\Repo\FactureSst\FactureSstInterface');
}
public function tearDown()
{
parent::tearDown();
m::close();
}
public function mock($class)
{
$mock = m::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
public function testFindingBySstAndBdc()
{
$this->mock->shouldReceive('where')->with('id_sst', 10)->once()->andReturn($this->mock);
$this->mock->shouldReceive('whereHas')->with('facture_assoc')->once()->andReturn($this->mock);
$this->mock->shouldReceive('get');
$result = $this->repo->findWithSstAndBdc(30207, 10);
$this->assertEquals($result, new \Illuminate\Support\Collection);
$this->assertEquals($result->count(), 0);
}
}
正如您在测试中看到的那样,我只是尝试调用函数并确保函数正确链接。但是我不断收到错误消息:
App\Tests\Unit\Api\FactureSst\FactureSstTest::testFindingBySstAndBdc Mockery\Exception\InvalidCountException:应调用 Mockery_0_FD_Repo_FactureSst_FactureSstInterface 中的方法 where("id_sst", 10) 正好 1 次,但调用了 0 次。
请有人帮我理解为什么这不起作用以及如何解决它。抱歉,代码是法语的,该应用程序是针对法语客户端的。
提前致谢。
【问题讨论】:
标签: laravel mocking phpunit repository-pattern mockery