我正在为一个客户端开发一个应用程序,但在测试存储库时遇到了麻烦。
要将存储库绑定到模型,我有以下代码:
<?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));
});
}
}
然后,存储库扩展一个抽象类,其中包含来自雄辩的ORM的函数(find、where、all等)。)。存储库的代码如下所示:
<?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的(“id_sst”,10)应被精确调用1次,但被调用0次。
请有人能帮助我理解为什么这是不工作,以及如何修复它。对不起,代码是法语,该应用程序是为一个法国客户。
先谢谢你了。
3条答案
按热度按时间uxh89sit1#
看起来你是在嘲笑仓库本身,而你真的应该嘲笑仓库的依赖关系,即
Illuminate\Database\Eloquent\Model
,你要访问数据库。更改
setUp()
,使其创建Illuminate\Database\Eloquent\Model
的模拟对象,然后在示例化存储库时注入该模拟对象。这有点令人困惑,因为您声明抽象类包含ORM方法,但当您调用这些方法时,您正在调用注入的
Model
依赖项,而不是抽象类。这可能就是混淆之处。此外,如果您的模型扩展了
Illuminate\Database\Eloquent\Model
,那么通常最好只将您的模型注入到存储库中,而不是Illuminate\Database\Eloquent\Model
。这样,您还可以利用在存储库中的模型中设置的任何关系函数。xdnvmnnf2#
如果我没记错的话
应该是
还请记住,您可以模拟整个调用链(对于像这样的流畅查询很有用):
我也会使用PHP内置的foreach()而不是使用$factures-〉each,因为这样就少了一个需要测试的东西。
nle07wnf3#
像下面这样试试