laravel 4嘲弄模拟模型关系

时间:2014-01-10 17:17:43

标签: unit-testing laravel mocking laravel-4 mockery

说我有两个从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
    }
}

1 个答案:

答案 0 :(得分:7)

由于track是属性而不是方法,因此在创建模拟时,您需要覆盖模型的setAttributegetAttribute方法。以下是一个解决方案,可让您设置您正在寻找的房产的期望:

$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);