说我有两个从Eloquent
延伸的模型,它们彼此相关。我可以嘲笑这段关系吗?
即:
class Track extends Eloquent {
public function courses()
{
return $this->hasMany('Course');
}
}
class Course extends Eloquent {
public function track()
{
return $this->belongsTo('Track');
}
}
在MyTest中,我想创建一个模拟当然,并通过调用track属性返回一个track实例,,而不是跟踪实例(我不想查询助洗剂)
use \Mockery as m;
class MyTest extends TestCase {
public function setUp()
{
$track = new Track(array('title' => 'foo'));
$course = m::mock('Course[track]', array('track' => $track));
$track = $course->track // <-- This should return my track object
}
}
答案 0 :(得分:7)
由于track是属性而不是方法,因此在创建模拟时,您需要覆盖模型的setAttribute
和getAttribute
方法。以下是一个解决方案,可让您设置您正在寻找的房产的期望:
$track = new Track(array('title' => 'foo'));
$course = m::mock('Course[setAttribute,getAttribute]');
// You don't really care what's returned from setAttribute
$course->shouldReceive('setAttribute');
// But tell getAttribute to return $track whenever 'track' is passed in
$course->shouldReceive('getAttribute')->with('track')->andReturn($track);
模拟track
对象时,不需要指定Course
方法,除非您还想测试依赖于查询构建器的代码。如果是这种情况,那么您可以像这样模拟track
方法:
// This is just a bare mock object that will return your track back
// whenever you ask for anything. Replace 'get' with whatever method
// your code uses to access the relationship (e.g. 'first')
$relationship = m::mock();
$relationship->shouldReceive('get')->andReturn([ $track ]);
$course = m::mock('Course[track]');
$course->shouldReceive('track')->andReturn($relationship);