Cakephp 2.1 に切り替えて、単体テストの学習を始めたところです...
私がArticlesControllerTestCase::testView()
そして走る
$this->testAction('articles/view/1');
debug($this->view);
debug($this->contents);
die;
どちらも$this->view and $this->contents
ヌルに等しい
beforeFilter 呼び出しと beforeRender 呼び出しを空にして、それを排除しようとしました...
$this->render('view');
コントローラーのアクションで、メソッドの最後に設定すると、$this->view
= 'それが想定されているもの' を取得しますが、$this->contents
= '同じものになり、レイアウトが含まれていないことに気付きました
なぜこれが起こっているのでしょうか?
class ArticlesController extends MastersController {
/*
* Name
*/
public $name = 'Articles';
/*
* Publicly Accessible Methods
*/
public $allowed = array('view', 'index', 'category', 'news');
/*
* Default Search Params
*/
public $presetVars = array(
array('field' => 'title', 'type' => 'value'),
array('field' => 'category_id', 'type' => 'value'),
array('field' => 'status_id', 'type' => 'value'),
);
/*
* Admin Menu Options
*/
public $adminMenu = array('index'=>array('Category'));
/**
* Before Filter Callback
* (non-PHPdoc)
* @see controllers/MastersController::beforeFilter()
* @return void
*/
public function beforeFilter(){
parent::beforeFilter();
$categories = $this->Article->Category->find('class', array('article', 'conditions'=>array('not'=>array('Category.name'=>'Content'))));
$this->set(compact('categories'));
}
/**
* View
* @param $id
* @see controllers/MastersController::_view()
* @return void
*/
public function view($id){
parent::_view($id);
$articleTitle = $this->Article->findField($id,'title');
$recentNews = $this->Article->find('recentNews');
$this->set('title_for_layout', $articleTitle);
$this->set(compact('recentNews'));
}
};
class MastersController extends AppController {
/*
* Name
*/
public $name = 'Masters'; # expected to be overridden
/**
* View
* Default view method for all controllers
* Provides an individual record based on the record id
* @param int $id: model id
* @return void
*/
protected function _view($id){
$this->Redirect->idEmpty($id, array('action'=>'index'));
${Inflector::singularize($this->request->params['controller'])} = $this->{$this->modelClass}->find('record', array($id));
${Inflector::variable(Inflector::singularize($this->request->params['controller']))} = ${Inflector::singularize($this->request->params['controller'])};
if(empty(${Inflector::singularize($this->request->params['controller'])})){
return $this->Redirect->flashWarning('Invalid Id.', $this->referer());
}
$this->set(compact(Inflector::singularize($this->request->params['controller']), Inflector::variable(Inflector::singularize($this->request->params['controller']))));
}
}
class ArticlesControllerTestCase extends ControllerTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array('app.article');
/**
* Set Up
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Articles = new TestArticlesController();
$this->Articles->constructClasses();
}
/**
* Tear Down
*
* @return void
*/
public function tearDown() {
unset($this->Articles);
parent::tearDown();
}
/**
* Test View
*
* @return void
*/
public function testView() {
$this->testAction('articles/view/1');
debug($this->view); die;
}
}
class ArticleFixture extends CakeTestFixture {
/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'slug' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 120, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'title' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'body' => array('type' => 'text', 'null' => false, 'default' => NULL, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'status_id' => array('type' => 'integer', 'null' => false, 'default' => NULL),
'category_id' => array('type' => 'integer', 'null' => false, 'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);
/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => '1',
'slug' => NULL,
'title' => 'Test Article #1 without slug - published',
'body' => '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p><p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>',
'created' => '2011-02-15 21:14:03',
'modified' => '2011-02-15 21:14:03',
'status_id' => '1',
'category_id' => '5'
),
);
}