2014-09-03 57 views
0

我在我的表文件中有以下操作。Cakephp 3如何使用TestSuite測試更新查詢?

public function tokenChange($token){ 
     if(!$token) return false; 
     return $this->query() 
      ->update() 
      ->set(['email_token' => NULL, 'email_verified'=>1, 'email_token_expires'=>NULL]) 
      ->where(['email_token' => $token]) 
      ->execute(); 
    } 

這是我TableTest

public function testTokenChange() 
{ 
      $result = $this->Users->tokenChange('5404f27f3d9a4'); 
      $expected = TRUE; 
      debug($result); 
      $this->assertEquals($expected,$result); 
} 

動作我知道查詢不返回一個布爾值。但是,如何檢查更新是否發生而無需運行其他查詢來檢查表?

回答

2

你基本上會測試該修改的行修改爲你所期望的:

public function testTokenChange() 
{ 
    $id = $this->Users->findByEmailToken('5404f27f3d9a4')->first()->id; 
    $this->Users->tokenChange('5404f27f3d9a4'); 
    $row = $this->Users->get($id); 
    $expected = [ 
     'email_token' => null, 
     'email_verified' => true, 
     'email_token_expires' => null 
    ]; 
    $this->assertEquals($expected, $row->extract(array_keys($expected))); 
}