0
我使用Cake bake
命令爲控制器創建了一個測試。測試不起作用CakePHP2.0
現在,我想測試控制器的功能「指數」,併爲它我這樣做:
public function testIndex() {
echo "printed";
$result = $this->testAction("/comments/1");
echo "not printed";
}
1帕拉姆,這裏的評論是帖子的ID。無論如何,控制器工作得很好,沒有問題。
如您所見,測試在調用testAction方法後崩潰。 (它不打印第二個回聲)
我已經看到,如果在控制器上調用的操作對其模型有任何調用,testAction調用將不起作用。但是,如果測試動作沒有任何對任何模型的調用,那麼它完美地起作用。
這裏發生了什麼? 順便說一句,這兩個數據庫,默認和測試都有數據,所以它不是數據庫的問題。
謝謝。
UPDATE: 這裏你有中的TestController通過烘烤蛋糕命令生成的休息:
<?php
/* Comments Test cases generated on: 2012-04-12 11:49:17 : 1334224157*/
App::uses('CommentsController', 'Controller');
/**
* TestCommentsController *
*/
class TestCommentsController extends CommentsController {
/**
* Auto render
*
* @var boolean
*/
public $autoRender = false;
/**
* Redirect action
*
* @param mixed $url
* @param mixed $status
* @param boolean $exit
* @return void
*/
public function redirect($url, $status = null, $exit = true) {
$this->redirectUrl = $url;
}
}
/**
* CommentsController Test Case
*
*/
class CommentsControllerTestCase extends CakeTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array('app.comment');
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Comments = new TestCommentsController();
$this->Comments->constructClasses();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
unset($this->Comments);
parent::tearDown();
}
你的testAction()方法在哪裏?碼? – thecodeparadox 2012-04-12 11:34:15
很高興看到測試用例的其餘部分(即父類,嘲笑,導入等)。 – jeremyharris 2012-04-12 14:11:49
我不知道,它是CakePHP中定義的一個方法,可用於測試。它應該可以工作:http://book.cakephp.org/2.0/en/development/testing.html#testing-controllers – Alvaro 2012-04-12 14:16:16