Home PageКаталог Изменения НовыеКомментарии Пользователи Регистрация
CakePHP: Manual/CommonTasks/Testing/TestingControllers ...
Это старая версия Manual/CommonTasks/Testing/TestingControllers за 2009-04-28 22:04:40..

Тестирование контроллеров

Создание тестовой оболочки


Допустим у нас есть типичный контроллер, с соответствующей моделью:


<?php 
class ArticlesController extends AppController 
   var 
$name 'Articles'
   var 
$helpers = array('Ajax''Form''Html'); 
   
   function 
index($short null) { 
     if (!empty(
$this->data)) { 
       
$this->Article->save($this->data); 
     } 
     if (!empty(
$short)) { 
       
$result $this->Article->findAll(null, array('id'
          
'title')); 
     } else { 
       
$result $this->Article->findAll(); 
     } 
 
     if (isset(
$this->params['requested'])) { 
       return 
$result
     } 
 
     
$this->set('title''Articles'); 
     
$this->set('articles'$result); 
   } 

?>


Создадим файл articles_controller.test.php в каталоге app/tests/cases/controllers и поместим в него следующее:


<?php 
class ArticlesControllerTest extends CakeTestCase 
   function 
startCase() { 
     echo 
'<h1>Starting Test Case</h1>'
   } 
   function 
endCase() { 
     echo 
'<h1>Ending Test Case</h1>'
   } 
   function 
startTest($method) { 
     echo 
'<h3>Starting method ' $method '</h3>'
   } 
   function 
endTest($method) { 
     echo 
'<hr />'
   } 
   function 
testIndex() { 
     
$result $this->testAction('/articles/index'); 
     
debug($result); 
   } 
   function 
testIndexShort() { 
     
$result $this->testAction('/articles/index/short'); 
     
debug($result); 
   } 
   function 
testIndexShortGetRenderedHtml() { 
     
$result $this->testAction('/articles/index/short'
     array(
'return' => 'render')); 
     
debug(htmlentities($result)); 
   } 
   function 
testIndexShortGetViewVars() { 
     
$result $this->testAction('/articles/index/short'
     array(
'return' => 'vars')); 
     
debug($result); 
   } 
   function 
testIndexFixturized() { 
     
$result $this->testAction('/articles/index/short'
     array(
'fixturize' => true)); 
     
debug($result); 
   } 
   function 
testIndexPostFixturized() { 
     
$data = array('Article' => array('user_id' => 1'published' 
          
=> 1'slug'=>'new-article''title' => 'New Article''body' => 'New Body')); 
     
$result $this->testAction('/articles/index'
     array(
'fixturize' => true'data' => $data'method' => 'post')); 
     
debug($result); 
   } 

?>



Метод testAction


Первый аргумент метода – Cake url действия контроллера, которое будет тестироваться, например '/articles/index/short'.


Второй аргумент – это массив параметров, состоящий из:


 
Комментарии
Добавить комментарий:

Файлов нет. [Показать файлы/форму]