我正在编写一些单元测试,并努力在Yii2中捕获这个小模型的剩余1行。
UserSearch.php
public function search($params)
{
$query = User::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// $query->where('0=1');
return $dataProvider; // This line in tests is red and marked as not executed
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'date_added' => $this->date_added,
'last_login' => $this->last_login,
]);
$query->andFilterWhere(['like', 'username', $this->username])
return $dataProvider;
}
UserTest.php
public function testUserSearch()
{
$model = new UserSearch();
expect_that($model->search(['id' => 2]));
}
public function testInvalidDataProvider()
{
$model = new UserSearch();
expect_that($model->search(['id' => '2']));
}
第二个测试正确传递,!this->Validate()
方法失败,因为id不是整数,为什么返回语句不反映为在代码覆盖中执行。我在这里误解了什么?
答案 0 :(得分:2)