laravel 4多对多关系集成测试未通过


laravel 4 many to many relationship integration test not passing

每个实体都被保存,并且都有一个ai id。只有第34行的测试没有通过。

<?php
class AssignmentTest extends TestCase
{
    protected $assignment;
    public function setUp()
    {
        parent::setUp();
        $this->assignment = new Assignment;
    }
    public function testAssignmentAssociations()
    {
        $facility = Facility::create(array(
        'name' => 'test facility',
        'code' => 'test-facility'
        ));
        $user = new User;
        $user->id = 1;
        $shift = Shift::create(array(
        'name' => 'test shift',
        'start_time' => 0,
        'end_time' => 600
        ));
        $this->assignment->facilities()->save($facility);
        $this->assignment->shift()->associate($shift);
        $this->assignment->user()->associate($user);
        $this->assignment->save();
        $this->assertEquals(1, $facility->id);
        $this->assertEquals(1, $this->assignment->facilities()->count());
        $this->assertEquals(1, $this->assignment->shift_id);
        $this->assertEquals(1, $this->assignment->user_id);
    }
}
// Facility model
public function assignments()
{
    return $this->belongsToMany('Assignment', 'assignment_facilities');
}
// Assignment model
public function facilities()
{
    return $this->belongsToMany('Facility', 'assignment_facilities');
}

编辑

显然,去掉这条线会使测试成功。现在我很感兴趣的是,为什么在保存分配时,关联将不再返回成功计数?

<?php
$this->assignment->save();

我还没有测试过,但在尝试将轮班和用户记录与分配关联之前,你不需要"保存"它们吗?

    $user = new User;
    $user->id = 1;
    $shift = Shift::create(array(
    'name' => 'test shift',
    'start_time' => 0,
    'end_time' => 600
    ));
    $shift->id = 1;

    $this->assignment->facilities()->save($facility);
    $this->assignment->shift()->save($shift);
    $this->assignment->user()->save($user);
    $this->assignment->shift()->associate($shift);
    $this->assignment->user()->associate($user);
    $this->assignment->save();