laravel 4多对多关系整合测试不通过

时间:2013-07-12 23:05:02

标签: php laravel laravel-4

每个实体都被保存并具有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();

1 个答案:

答案 0 :(得分:0)

我还没有测试过 - 但是在尝试将它们与作业相关联之前,您是否需要“保存”班次和用户记录?

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