2016-11-16 2 views
0

私はいくつかの単体テストを書いており、この小さなモデルの残りの1行を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()メソッドが失敗したように、第2の試験が理由ではなく、正しく通過return文はコードカバレッジで実行されたとおりに反映されます。私はここで何を誤解していますか?

+0

「validate」メソッドのコードを表示してください。 'expect_that'は奇妙なアサーションです。どうしますか? – Naktibalda

答えて

2
+0

はい、私は知っているので、文if(!$ this-> validate())は正しく失敗します。なぜコードカバレッジに反映されないのですか? – Jonnny

+0

この行のコメントを外す必要があります// $ query-> where( '0 = 1'); – nicolascolman

関連する問題