单元测试

时间:2016-07-26 08:45:58

标签: unit-testing cakephp phpunit

我是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'来处理我的Model函数中的条件,但我无法让'data'对象起作用,所以我切换到了fixture。 ......没有成功。

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单元测试,所以请随意解释我出错的地方。

谢谢!

1 个答案:

答案 0 :(得分:2)

您需要稍微调整一下您的模型函数(我将在下面显示),但是您应该能够执行类似这样的操作来传递数据对象中的数据:

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

这不是&#34; set&#34;你用过,如下:

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

此外,您嘲笑了客户端模型,然后&#34;设置&#34;一些事情,但在你做测试之前,你再次嘲笑它。这意味着你要丢弃你为顶部所做的模拟设置的所有细节。您可以执行以下操作来解决您的问题:

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 );
}

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