3

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

我的 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

大法错了吗?谢谢!

4

1 回答 1

13

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

App::uses('AppController', 'Controller');
于 2012-11-22T15:11:03.690 回答