doufan6033 2014-03-31 22:28
浏览 52

CakePHP单元测试控制器返回未定义的变量

I am new to the Unit Testing portion of CakePHP. There is no better time to learn than now.

I have ready through all of the documentation and numerous tutorials on the web. I have run up against a snag during the completion of some of my more basic unit tests.

My Controller:

class TestsController extends AppController {

    public $components = array('RequestHandler', 'Security');

    public $helpers = array('Js');

    public $uses = array('Tests');

    public function beforeFilter() {
        $this->layout = 'admin';
        parent::beforeFilter();
    }

/**
 * Returns database values of all tests that have been created.
 *
 */
    public function index() {
        if ($this->request->is('requested')) {
            return $tests;
        }
        $this->paginate = array(
            'fields' => array('id', 'name', 'email', 'access_token', 'access_token_begins',
                'filesizelimit', 'related_files', 'access_token_expires'),
            'limit' => 10,
            'order' => array(
                'created' => 'desc'
        ));
        $this->set('tests', $this->paginate('Test'));
    }

My Controller test looks like this:

    class TestsControllerTest extends ControllerTestCase {

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

    public function testIndex() {

        $result = $this->testAction('tests/index');
        debug($result);
    }
    }

The output of my test is:

PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined variable: tests
Test case: testsControllerTest(testIndex)

The only viable solution to the error I am receiving would be that maybe the test controller needs authentication before it can actually run the testAction() ?

This is a very simple sample from the CakePHP Documentation guide, yet I receive and undefined variable error. Any help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongyuhui0418 2014-04-01 17:22
    关注

    I have managed to answer my own question.

    The $tests variable is undefined because of the if statement within the controller:

    public function index() {
        if ($this->request->is('requested')) {
            return $tests;
        }
        $this->paginate = array(
            'fields' => array('id', 'name', 'email', 'access_token', 'access_token_begins',
                'filesizelimit', 'related_files', 'access_token_expires'),
            'limit' => 10,
            'order' => array(
                'created' => 'desc'
        ));
        $this->set('tests', $this->paginate('Test'));
    }
    

    That statement does not get hit when running the application, but it does when running the test. In this given function there is no need for the IF statement and therefore my error of UNDEFINED VARIABLE is now resolved.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数