【问题标题】:CakePHP unit test code coverage $this->request->data clause not executedCakePHP 单元测试代码覆盖 $this->request->data 子句未执行
【发布时间】:2013-06-14 22:06:40
【问题描述】:

我对 PHP 单元测试(使用 PHPUnit)和 CakePHP(2) 作为一个框架完全陌生,5 年后我又回到了 PHP。

我已经建立并运行了一个网站,并且正在编写单元测试作为最佳实践。但是,xdebug 表明当我相信我正在调用它时,我的其中一个子句没有被涵盖,我只是不明白为什么。我已经用谷歌搜索了我能想到的所有搜索词,并重新阅读了食谱的相关部分,并且(虽然我学到了很多其他有用的东西)我没有找到答案,所以希望知情人士将给出一个简单的答案:)

以下是相关的代码部分:

控制器:

<?php

App::uses('AppController', 'Controller');

// app/Controller/ClientsController.php
class ClientsController extends AppController {

/* other functions */
 public function edit($id = null) {
            if (!$id) {
                    $this->Session->setFlash(__('Unable to find client to edit'));
                    return $this->redirect(array('action'=>'index'));
            }
            $client = $this->Client->findById($id);
            if(!$client) {
                    $this->Session->setFlash(__('Unable to find client to edit'));
                    return $this->redirect(array('action'=>'index'));
            }

            if ($this->request->is('post')) {
                    $this->Client->id = $id;
                    if ($this->Client->saveAll($this->request->data)) {
                            $this->Session->setFlash(__('Client has been updated.'));
                            return $this->redirect(array('action'=>'index'));
                    } else {
                            $this->Session->setFlash(__('Unable to update client'));
                    }
            }

            if (!$this->request->data) {
                    $this->request->data = $client;
                    $this->Session->setFlash(__('Loading data'));
                }
        }
}

测试:

<?php

// Test cases for client controller module
class ClientsControllerTest extends ControllerTestCase {

    public $fixtures = array('app.client');

    /* other tests */
        public function testEdit() {

            // Expect success (render)
            $result = $this->testAction('/Clients/edit/1');
            debug($result);
         }
    }
?>

代码按预期执行。如果我浏览到“/Clients/edit/1”,会显示我期望的 Flash 消息(正在加载数据),表明没有请求数据,因此它是从 $client 加载的。正确的数据显示在编辑表单中。

当我从测试中调用时,我收到一条成功消息,表明测试已通过,但 xdebug 代码覆盖率显示 if (!$this-&gt;request-&gt;data) { .. } 子句未被覆盖,并且没有明显的错误。

这对我来说似乎违反直觉,因此希望避免对未来(更复杂的)单元测试感到沮丧 - 任何人都可以解释为什么在正常访问页面期间调用该子句时测试会通过但不执行该子句?

(在我尝试编辑之前,夹具在数据结构和插入数据方面都是正确的。从没有 id 或无效 id 的测试用例调用 edit() 可以正确执行相关子句,同样如此传递未通过验证的数据。)

【问题讨论】:

    标签: cakephp phpunit xdebug


    【解决方案1】:

    我遇到了类似的问题,我通过在 testAction() 中添加第二个参数来解决:

    $this->testAction('/Clients/edit/1', array('method' => 'get'));
    

    你也可能想改变你的

    if ($this->request->is('post') {
        ...
    }
    if (!$this->request->data) {
        ...
    }
    

    收件人:

    if ($this->request->is('post') {
        ...
    } else {
        ...
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2012-01-18
      • 2010-10-14
      • 2013-04-16
      • 2014-12-10
      相关资源
      最近更新 更多