cakephp ControllerTest

时间:2012-11-20 17:13:01

标签: cakephp testing

我是一个菜鸟,试图为cakephp书的博客教程编写一个ControllerTest。 完成这项任务后,我找到了一个很好的例子,我可以适应。 这本书提供了以下示例: http://book.cakephp.org/2.0/en/development/testing.html#testing-controllers 所以我在/ app / Controller /中创建了一个ArticlesController.php文件,在/ app / Test / Case / Controller /

中创建了一个ArticlesControllerTest.php

我的ArticlesController.php的内容是:

<?php
class ArticlesController extends ControllerTestCase {
//public $fixtures = array('app.article');

public function testIndex() {
    $result = $this->testAction('/articles/index');
    debug($result);
}

public function testIndexShort() {
    $result = $this->testAction('/articles/index/short');
    debug($result);
}

public function testIndexShortGetRenderedHtml() {
    $result = $this->testAction(
        '/articles/index/short',
        array('return' => 'contents')
    );
    debug($result);
}

public function testIndexShortGetViewVars() {
    $result = $this->testAction(
        '/articles/index/short',
        array('return' => 'vars')
    );
    debug($result);
}

public function testIndexPostData() {
    $data = array(
        'Article' => array(
            'user_id' => 1,
            'published' => 1,
            'slug' => 'new-article',
            'title' => 'New Article',
            'body' => 'New Body'
        )
    );
    $result = $this->testAction(
        '/articles/index',
        array('data' => $data, 'method' => 'post')
    );
    debug($result);
}

}

我的ArticlesController.php的内容是:

<?php
class ArticlesControllerTest extends ControllerTestCase {
    public $fixtures = array('app.article');

    public function testIndex() {
        $result = $this->testAction('/articles/index');
        debug($result);
    }

    public function testIndexShort() {
        $result = $this->testAction('/articles/index/short');
        debug($result);
    }

    public function testIndexShortGetRenderedHtml() {
        $result = $this->testAction(
           '/articles/index/short',
            array('return' => 'contents')
        );
        debug($result);
    }

    public function testIndexShortGetViewVars() {
        $result = $this->testAction(
            '/articles/index/short',
            array('return' => 'vars')
        );
        debug($result);
    }

    public function testIndexPostData() {
        $data = array(
            'Article' => array(
                'user_id' => 1,
                'published' => 1,
                'slug' => 'new-article',
                'title' => 'New Article',
                'body' => 'New Body'
            )
        );
        $result = $this->testAction(
            '/articles/index',
            array('data' => $data, 'method' => 'post')
        );
        debug($result);
    }
}

我从书中复制了这些代码,并对夹具进行了评估。 运行测试给了我以下错误:

错误:未找到类'AppController' 文件:/Applications/MAMP/htdocs/cake/app/Controller/ArticlesController.php
行:3

Dafaq错了? Thxs!

1 个答案:

答案 0 :(得分:13)

尝试将以下内容添加到ArticlesController文件的顶部:

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