【问题标题】:Pass data variable to function when Unit Testing单元测试时将数据变量传递给函数
【发布时间】:2016-11-29 19:38:26
【问题描述】:

我是 PHP 单元测试的新手,遇到了一些麻烦。无论是因为我使用的是 Cake 框架还是因为我习惯了 Java 方式,都指出我遇到了问题。

我正在为在提交表单时调用的模型函数编写测试。该函数接收两个我认为我正确传递的参数,以及一个未作为参数接收的数据对象。我的问题是如何填充该“数据”对象?运行测试时,我不断收到“未定义索引”错误。

我已经尝试过模拟数据和使用固定装置,但老实说,我不明白这些东西。下面是我的模型函数,后面是我的测试代码。

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 版本,所以你可以随意使用它。我在想模拟数据会更容易和更快,因为我的模型函数中的条件只需要'client_type_id',但我无法让那个'数据'对象工作,所以我切换到了固定装置。 ..没有成功。

public function testIsUniqueIfVerified01() {
    $this->Client = $this->getMock ( 'Client', array (
            'find' 
    ) );
    $this->Client->set(array(
                'client_type_id' => 1,
                'identity_no' => 1234567890123
    ));
    //$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 单元测试,所以请随时解释我哪里出错了。

谢谢!

【问题讨论】:

    标签: unit-testing cakephp phpunit


    【解决方案1】:

    您需要对模型函数(我将在下面显示)进行轻微调整,但随后您应该能够执行以下操作来传递数据对象中的数据:

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

    这不是您使用的“集合”,如下所示:

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

    此外,您模拟了 Client 模型,然后“设置”了一些东西,但是在您进行测试之前,您再次模拟了它。这意味着您将扔掉为您在顶部所做的模拟设置的所有内容。您可以执行以下操作来解决您的问题:

    public function testIsUniqueIfVerified01() {
        $this->Client = $this->getMock ( 'Client', array (
            'find' 
        ) );
        $this->Client->data = array(
            'Client' => array(
                'client_type_id' => 5,
                'identity_no' => 1234567890123
        ));
    
        $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 );
    }
    

    这至少应该让您知道该怎么做。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多