我正在編寫一些單元測試,並努力在Yii2中捕獲這個小模型的剩餘一行。我將如何測試代碼中的這一行
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']));
}
第二測試通過正確地爲作爲ID不是整數!this->Validate()
方法失敗,爲什麼不返回語句反映爲在代碼覆蓋範圍內執行。我在這裏誤解了什麼?
請顯示驗證方法的代碼。 'expect_that'是一個奇怪的斷言,它有什麼作用? – Naktibalda