0

我正在尝试为文章控制器测试非常简单的添加操作。下面是我的代码。我不明白为什么这不起作用。

$this->Articles->Article->hasAny

应该为我刚刚调用的 admin_add 操作和我刚刚添加的测试记录返回 true。但是它返回错误;

    class ArticlesControllerTestCase extends ControllerTestCase {

    /**
         * Test Admin Add
         *
         * @return void
         */
        public function testAdminAdd() {   

            #define sample passing data  
            $sampleDataPass = array(
                'Article'=>array(
                    'title'=>'---Test Article Add---',
                    'body'=>'---Test Article Add Body---',
                    'status_id'=>1,
                    'category_id'=>1,
                )
            );

            #test passing data
            $this->Articles->request->data = $sampleDataPass;
            $this->testAction('admin/articles/add', array('data'=>$sampleDataPass));
            $this->assertEquals($this->Articles->Article->hasAny(array('Article.title'=>'---Test Article Add---')), true); #fails here
            }
    }


class MastersController extends AppController {
/**
     * Admin Add
     * Default admin add method for all controllers
     * Adds a record to the models database table
     * Not calling add action becase of __setRootUserId action
     * @return void
     */
    protected function _admin_add() {
        if (!empty($this->request->data)) { 
            #save data
            $this->{$this->modelClass}->create();
            if ($this->{$this->modelClass}->save($this->request->data)) {
                return $this->Redirect->flashSuccess($this->modelClass.' saved.', isset($this->redirectSuccessUrl) ? $this->redirectSuccessUrl : array('action' => 'view', $this->{$this->modelClass}->id));
            } else {
                return $this->Redirect->flashWarning($this->modelClass.' could not be saved.', isset($this->redirectWarningUrl) ? $this->redirectWarningUrl : null);
            }
        }
        $this->_lists();
    }
}

class ArticlesController extends MastersController {
    /**
     * Admin Add
     * @see controllers/MastersController::_admin_add()
     * @return void
     */
    public function admin_add(){
        parent::_admin_add();       
    }
}
4

1 回答 1

0

缓存$this->Article$this->controller仅在调用 testAction 后有效)。如果您之前需要调用它,则需要$this->generate()在执行任何操作之前调用以获取控制器实例

于 2012-05-23T00:56:44.017 回答