cakephp ControllerTest

Bin*_*ary 3 testing cakephp

我是一个菜鸟,试图为Cakephp书的Blog Tutorial编写一个ControllerTest.完成这项任务后,我找到了一个很好的例子,我可以适应.本书提供了以下示例:http: //book.cakephp.org/2.0/en/development/testing.html#testing-controllers 因此,我在/ app/Controller /中创建了一个ArticlesController.php文件,并在其中创建了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);
}
Run Code Online (Sandbox Code Playgroud)

}

而我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

大法错了?Thxs!

Lis*_*saF 13

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

App::uses('AppController', 'Controller');
Run Code Online (Sandbox Code Playgroud)

  • 如果答案被接受会更好:) (2认同)
  • 在我能够:)之后,我回来了,投票并接受了答案 (2认同)