Php单元测试没有覆盖Yii2模型的所有属性标签
当我使用phpunit进行测试时,我正在为Yii2编写测试代码。那么代码覆盖率检查并没有完全覆盖属性标签方法。它只包括第一行。这是我下面给出的测试模型代码Php单元测试没有覆盖Yii2模型的所有属性标签,yii2,phpunit,code-coverage,yii2-basic-app,Yii2,Phpunit,Code Coverage,Yii2 Basic App,当我使用phpunit进行测试时,我正在为Yii2编写测试代码。那么代码覆盖率检查并没有完全覆盖属性标签方法。它只包括第一行。这是我下面给出的测试模型代码 public function testattributeLabels() { $attribute = Academicrecords::attributeLabels(); print_r($attribute); $this->assertInternalType('array',$attribute);
public function testattributeLabels()
{
$attribute = Academicrecords::attributeLabels();
print_r($attribute);
$this->assertInternalType('array',$attribute);
$this->assertContains('Student Name',$attribute);
$this->assertEquals('Student Name',$attribute['student_id']);
$this->assertEquals('ID',$attribute['id']);
$this->assertEquals('School Name',$attribute['school_name']);
$this->assertEquals('Class',$attribute['class']);
$this->assertEquals('Stream',$attribute['stream']);
$this->assertEquals('Created On',$attribute['created_on']);
$this->assertEquals('Modified On',$attribute['modified_on']);
$this->assertEquals('Created By',$attribute['created_by']);
$this->assertEquals('Modified By',$attribute['modified_by']);
}
这是我的模型代码
public function attributeLabels()
{
return [
'id' => 'ID',
'student_id' => 'Student Name',
'school_name' => 'School Name',
'class' => 'Class',
'stream' => 'Stream',
'created_by' => 'Created By',
'created_on' => 'Created On',
'modified_by' => 'Modified By',
'modified_on' => 'Modified On',
];
}
这是我的codeception结果
因为attributeLabels()不是静态函数,所以您可能想试试这个。但不确定它是否解决了您的问题
public function testattributeLabels() {
$model = new Academicrecords();
$this->assertEquals('Student Name',$model->getAttributeLabel('student_id'));
$this->assertEquals('ID',$model->getAttributeLabel('id'));
$this->assertEquals('School Name',$model->getAttributeLabel('school_name'));
$this->assertEquals('Class',$model->getAttributeLabel('class'));
$this->assertEquals('Stream',$model->getAttributeLabel('stream'));
$this->assertEquals('Created On',$model->getAttributeLabel('created_on'));
$this->assertEquals('Modified On',$model->getAttributeLabel('modified_on'));
$this->assertEquals('Created By',$model->getAttributeLabel('created_by'));
$this->assertEquals('Modified By',$model->getAttributeLabel('modified_by'));
}