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