컨트롤러 메소드의 추가 동작을 테스트하고 싶습니다. 저장 작업 결과를 확인하고 싶습니다. 그래서 기본적으로 작업에 게시물을 보내고 요청이 끝난 후 찾기를 사용하여 테스트 결과를 확인하려고합니다. $ this-> testAction()이 올바른 방법이 아닌 것 같습니다 (아래 코드의 주석 참조). 어떻게해야합니까?CakePHP 컨트롤러 유닛 테스트
Public function add() {
.....
if ($this->request->is('post') && isset($this->request->data['InvoiceEntry'])) {
....
$this->request->data = $this->__someMethod($this->request->data);
if ($this->Invoice->saveAssociated($this->request->data)) {
....
$this->redirect(array('action' => 'index'));
}
.....
}
테스트 코드 :
public function testAdd() {
$data = array('Invoice' => array(...), 'InvoiceEntry' => array(....));
// Method 1
$this->testAction('/invoices/add/', array(
'method' => 'post',
'data' => $data,
));
// Unable to do find after testAction because testAction basically is the test?
// Method 2:
$this->controller = $this->generate('Invoices');
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->controller->requestAction('/invoices/add',array(
'data' => $data
));
// not working because requestAction() follows the redirect in the add method
}
나는 TestCase :: setUp()에서 모델을 초기화해야한다는 사실에 동의하지 않는다. 이를 수행하는 것이 유일한 이유는 테스트 기능의 80 %에서 모델을 사용할 것이라는 것입니다. 그렇지 않으면 당신은 단지 거의 사용하지 않을 객체를 생성 할 것입니다. 나쁜 습관이 아닌 나쁜 습관입니다. – chadrien