【发布时间】:2015-07-10 14:30:57
【问题描述】:
我在一些项目中使用 Laravel,我正在尝试为一个函数创建一个测试。这是我的控制器中的功能:
public function showThoseThings()
{
return parent::httpRequest(
function ($context) {
$context->results['item'] = $this->object->findOrFail(list_id)->getElements;
}
);
}
“getElements()”函数在模型中定义如下:
public function getElements()
{
return $this->belongsToMany(
'things',
'thing_to_list',
'list_id',
'thing_id'
)
->withPivot('position')
->orderBy('thing_to_list.position');
}
基本上在后端有三个表,一个列表表是事物的集合,然后是一个事物表,其中包含与列表关联的多个事物,然后我们有数据透视表。我怎么能用 Laravel 模拟这个。这是我一直在做的事情:
public function testShowThoseTHingsSuccess()
{
$this->mock->shouldReceive('findOrFail')->with(1)->andReturn($this->mock);
$this->mock->shouldReceive('getElements')->once()->andReturn($this->mock);
$response = $this->call('GET', 'workingURI');
var_dump($response);
$this->assertTrue($response->isOk());
但是在命令行中运行 phpunit 时,我得到了:
"Unknown Error","message":"SQLSTATE[HY000] [1045] Access denied for user... 无法断言 false 为真"。
【问题讨论】:
-
这可能会有所帮助:stackoverflow.com/a/21688413/1903366
标签: database model-view-controller laravel-4 phpunit mockery