2012-05-17 21 views
2

Swtiching一到CakePHP的2.1和剛開始學習單元測試...

當我ArticlesControllerTestCase::testView()

和運行

$this->testAction('articles/view/1'); 
debug($this->view); 
debug($this->contents); 
die; 

兩個$this->view and $this->contents等於null

我已經清空了beforeFilter呼叫和呼叫beforeRender嘗試和elimitate日在...

我注意到,在控制器動作,如果我在方法的末尾設置$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' 
     ), 
     ); 
} 
+0

'$ this'不會引用您的控制器,而是引用您的測試用例。 – pixelistik

回答

-1

我想我知道是什麼導致了這個問題。您需要提供testAction()的方法。

$this->testAction('articles/view/1', array('method'=>'get')); 
debug($this->view); //should output the view 
debug($this->contents); //should output the contents 

恕我直言,在文檔(http://book.cakephp.org/2.0/en/development/testing.html#testing-controllers)使它看起來像默認爲GET。默認實際上是POST - 因此「張貼」到articles/view/1會讓你一無所獲。

+0

也嘗試調試($ this-> headers) - 檢查你是否從頁面重定向 – wenbert

+0

仍然設置爲空...如果我設置$ this->呈現( '觀看');在控制器動作中,然後$ this-> view/$ this-> contents被正確設置。不過,我不應該這樣做,並且調試($ this-> headers)仍然是一個空數組。我必須儘快弄清楚這個問題,並在這個項目後面運行。 – devnull

+0

$ this->標題將爲空,如果你沒有重定向。如果你有$ this-> view/$ this->內容,那麼你應該只對這些恕我直言...斷言... 我是CakePHP的新手 - 如果你有更多的問題,然後#cakephp在freenode也是一個偉大的方式來問人們... – wenbert

0

如果您打算執行單元測試,您應該真正考慮測試每個單元。這就是說,PHPUnit集成到CakePHP中時相當健壯,可以提供您需要的東西,如數據和方法模擬。

當我寫一個單元測試,它看起來有點像這樣:

function testSomeTestCaseWithDescriptiveMethodName(){ 
    $this->testAction("controller/action/arguments"); 
    $this->assertEqual([some value or data set], $this->vars["key"]; 
} 

這將讓你對測試沒有做一個集成測試,其中將包括視圖,數據庫訪問創造的價值等

我希望這有助於。 :)

1

如果您被重定向,例如,某些認證代碼在會話數據不符合預期時重定向到登錄頁面,則會發生這種情況。如果您未在測試中設置所需的環境,則會重定向。

debug("Headers: " . print_r($this->headers)); 

如果你把你的測試,並看到類似array('Location' => 'your_login_page'),請確保您的測試環境包括所需的一切,讓您的身份驗證系統(或其他任何可能:您可以通過檢查$this->headers值檢查了這插入重定向)開心。

相關問題