2016-07-26 66 views
1

我是新來的單元測試在PHP中,我有點麻煩。無論是因爲我使用的是Cake框架,還是因爲我習慣了Java方式,指出我遇到了問題。當單元測試時傳遞數據變量的功能

我正在寫一個模型函數的測試,該函數在提交表單時被調用。該函數接收兩個參數,我認爲我正確通過了,並且沒有收到一個數據對象作爲參數。我的問題是如何填充該「數據」對象?當我運行測試時,我不斷收到「undefined index」錯誤。

我已經嘗試了嘲笑數據和使用燈具,但誠實地說,我沒有得到這個東西。以下是我的模型功能,其次是我的測試代碼。

public function isUniqueIfVerified($check, $unverified){ 
    $found = false; 
    if ($this->data['Client']['client_type_id'] == 5) { 
     $found = $this->find ('first', array (
      'conditions' => array (
       $check, 
       $this->alias . '.' . $this->primaryKey . ' !=' => $this->id, 
       'client_type_id <>' => 5 
      ), 
      'fields' => array (
       'Client.id' 
      ) 
     )); 
    } else { 
     $found = $this->find ('first', array (
       'conditions' => array (
         $check, 
         $this->alias . '.' . $this->primaryKey . ' !=' => $this->id 
       ), 
       'fields' => array (
         'Client.id' 
       ) 
     )); 
    } 

    if ($found) { 
     return false; 
    } else { 
     return true; 
    } 
} 

這就像我的測試功能的52版本,所以隨時隨地做任何你想做的事情。我在考慮嘲笑數據會更容易和更快,因爲我只需要'model_type_id'來處理我的Model函數中的條件,但是我無法使'data'對象工作,所以我切換到了燈具。 ..沒有成功。

public function testIsUniqueIfVerified01() { 
    $this->Client = $this->getMock ('Client', array (
      'find' 
    )); 
    $this->Client->set(array(
       'client_type_id' => 1, 
       'identity_no' => 123456789
    )); 
    //$this->Client->log($this->Client->data); 
    $check = array (
      'identity_no' => '1234567890123' 
    ); 
    $unverified = null; 

    $this->Client = $this->getMockforModel("Client",array('find')); 
    $this->Client->expects($this->once()) 
     ->method("find") 
     ->with('first', array (
       'conditions' => array (
         "identity_no" => "1234567890123", 
         "Client.id" => "7711883306236", 
         'client_type_id <>' => 5 
       ), 
       'fields' => array (
         'Client.id' 
       ) 
     )) 
     ->will($this->returnValue(false));  

    $this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified)); 

    unset ($this->Client); 
} 

再次說到Cake,我更加專注於PHP單元測試,所以請隨時解釋我出錯的地方。

謝謝!

回答

2

你需要做小幅調整到模型的功能(我將在下面顯示),但是你應該能夠做這樣的事情通過數據對象中的數據:

$this->Client->data = array(
    'Client' => array(
     'client_type_id' => 5, 
     'identity_no' => 123456789
)); 

這是不是「設置」您使用,如下:

$this->Client->set(array(... 

而且,你嘲笑的客戶端模型,然後在「設置」的幾件事情,但你做了測試之前,你嘲笑它再次。這意味着你正在拋棄所有爲你在頂部做的模擬而設置的東西。你可以做一些如下應該可以解決你的問題:

public function testIsUniqueIfVerified01() { 
    $this->Client = $this->getMock ('Client', array (
     'find' 
    )); 
    $this->Client->data = array(
     'Client' => array(
      'client_type_id' => 5, 
      'identity_no' => 123456789
    )); 

    $check = array (
     'identity_no' => '1234567890123' 
    ); 
    $unverified = null; 

    $this->Client->expects($this->once()) 
     ->method("find") 
     ->with($this->identicalTo('first'), $this->identicalTo(array(
      'conditions' => array (
       $check, 
       "Client.id !=" => 1, 
       'client_type_id <>' => 5 
      ), 
      'fields' => array (
       'Client.id' 
      ) 
     ))) 
     ->will($this->returnValue(false));  

    $this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified)); 

    unset ($this->Client); 
} 

這應該至少讓你知道該怎麼做。希望能幫助到你!