2009-08-07 32 views
5

SimpleTest 1.0.1 kullanarak CakePHP (yeni sürüm 1.2.4) ile yeni bir uygulama yazıyorum. Cookbook'un ilgili bölümlerini okudum, Bakery'da arama yaptım ve Denetleyici testinde (hard way ve with mocks) Mark Story'nin gönderilerini oku. Ne yazık ki, önemsiz olmayan kontrolörlerin gerçek dünya testi ile ilgili bu görüşmelerden hiçbiri maalesef bu görüşmelerden hiçbiri sözkonusu değildir. uygulamalar Birçok görüntüleyebilmek için giriş sitenin alanlarını koymak, henüz basit bir senaryoyu test etmek için nasıl bilemiyorum: korumalı sayfa yönlendirmelereCakePHP denetleyicilerinin gerçek dünya testi?

  • konuk erişimi?
  • Geçerli kimlik bilgileri beklenen oturum değişkenlerini ayarlıyor mu?
  • Geçersiz kimlik bilgileri hata iletisiyle giriş sayfasını yeniden görüntüler?

Aşağıdaki kontrolör ve testler, düşündüklerim gibi çalışmazlar. Hem iddialar başarısız ve ben de bir PHP hatası alıyorum:

[NULL] BAŞARISIZ [.../app/test/olgu/kontrolörleri/users_controller.test.php hattı 79] de boş olmamalı ... /app/tests/cases/controllers/users_controller.test.php -> UsersControllerTest -> testLogin

[NULL] uymuyor gibi Eşit beklenti başarısız BAŞARISIZ [Tamsayı: 1] [.../app/testleri /cases/controllers/users_controller.test.php hattı 80] .../app/test/olgu/kontrolörleri/users_controller.test.php -> UsersControllerTest -> testLogin

HATA Beklenmeyen PHP hatası [Tanımlanmamış dizin: eylem] [E_NOTICE] [.../cake/libs/controller/components/auth.php line 266] .../app/tests/cases/controllers/users_controller.test .php -> UsersControllerTest -> testLogin İşte

kontrolör (pişmiş artı Mark Story'nin "zor yoldan" test yöntemi):

/* SVN FILE: $Id$ */ 
/* UsersController Test cases generated on: 2009-08-05 17:08:03 : 1249507923*/ 
App::import('Controller', 'Users'); 

class TestUsers extends UsersController 
{ 
    var $autoRender = false; 
    var $redirectUrl; 
    var $redirectStatus; 
    var $renderedAction; 
    var $renderedLayout; 
    var $renderedFile; 
    var $stopped; 

    function redirect($url, $status = null, $exit = true) 
    { 
    $this->redirectUrl = $url; 
    $this->redirectStatus = $status; 
    } 

    function render($action = null, $layout = null, $file = null) 
    { 
    $this->renderedAction = $action; 
    $this->renderedLayout = (is_null($layout) ? $this->layout : $layout); 
    $this->renderedFile = $file; 
    } 

    function _stop($status = 0) 
    { 
    $this->stopped = $status; 
    } 
} 

class UsersControllerTest extends CakeTestCase 
{ 
    var $fixtures = array('user'); 
    var $Users = null; 

    function startTest() 
    { 
    $this->Users = new TestUsers(); 
    $this->Users->constructClasses(); 
    $this->Users->Component->initialize($this->Users); 
    } 

    function prepareForAction() 
    { 
    $this->Users->beforeFilter(); 
    $this->Users->Component->startup($this->Users); 
    } 

    function endTest() 
    { 
    $this->Users->Session->destroy(); 
    unset($this->Users); 
    ClassRegistry::flush(); 
    } 

    //----------------------------------------------------------------------- 

    function testUsersControllerInstance() 
    { 
    $this->assertTrue(is_a($this->Users, 'UsersController')); 
    } 

    function testLogin() 
    { 
    $this->Users->data = array(
     'User' => array(
     'username' => 'admin', 
     'password' => 'admin' 
    ) 
    ); 

    $this->prepareForAction(); 
    $this->Users->login(); 

    $this->assertNotNull($this->Users->redirectUrl); 
    $this->assertEqual($this->Users->Session->read('Auth.User.id'), 1); 
    } 
} 

cevap

4

testi: Burada

class UsersController extends AppController 
{ 
    var $name = 'Users'; 
    var $helpers = array('Html', 'Form'); 
    var $components = array('Auth'); 

    function login() 
    { 
    } 

    function logout() 
    { 
    $this->redirect($this->Auth->logout()); 
    } 

    function index() 
    { 
    $this->set('users', $this->paginate()); 
    } 

    function view($id = null) 
    { 
    if (!$id) 
    { 
     $this->Session->setFlash(__('Invalid User.', true)); 
     $this->redirect(array('action'=>'index')); 
    } 
    $this->set('user', $this->User->read(null, $id)); 
    } 

    function add() 
    { 
    if (!empty($this->data)) 
    { 
     $this->User->create(); 
     if ($this->User->save($this->data)) 
     { 
     $this->Session->setFlash(__('The User has been saved', true)); 
     $this->redirect(array('action'=>'index')); 
     } 
     else 
     { 
     $this->Session->setFlash(__('The User could not be saved. Please, try again.', true)); 
     } 
    } 
    } 

    function edit($id = null) 
    { 
    if (!$id && empty($this->data)) 
    { 
     $this->Session->setFlash(__('Invalid User', true)); 
     $this->redirect(array('action'=>'index')); 
    } 
    if (!empty($this->data)) 
    { 
     if ($this->User->save($this->data)) 
     { 
     $this->Session->setFlash(__('The User has been saved', true)); 
     $this->redirect(array('action'=>'index')); 
     } 
     else 
     { 
      $this->Session->setFlash(__('The User could not be saved. Please, try again.', true)); 
     } 
    } 
    if (empty($this->data)) 
    { 
     $this->data = $this->User->read(null, $id); 
    } 
    } 

    function delete($id = null) 
    { 
    if (!$id) 
    { 
     $this->Session->setFlash(__('Invalid id for User', true)); 
     $this->redirect(array('action'=>'index')); 
    } 
    if ($this->User->del($id)) 
    { 
     $this->Session->setFlash(__('User deleted', true)); 
     $this->redirect(array('action'=>'index')); 
    } 
    } 
} 

testtir Gerçekten gerçekten UserContoller'ı test etmiyorsun, sen gerçekten Au'yu test ediyorsun. thComponent. Bunu yapmak istiyorsanız, TestUsersController'ınızı uygulamanızda yapacağınız şekilde ayarladığınızdan emin olmanız gerekir. Ben Mark's mock objects post başka bir göz atıyoruz ve denetleyici kodu için testler yazmak için bu yöntemleri kullanarak ve auth bileşeni alay öneririm,

function testLogin() 
{ 
$this->Users->data = array(
       'User' => array(
        'username' => 'admin', 
        'password' => 'admin' 
       ) 
      ); 

$this->Users->params['url']['url'] = '/users/login'; 
$this->Users->params['action'] = 'login'; 
$this->prepareForAction(); 
$this->Users->login(); 

$this->assertNotNull($this->Users->redirectUrl); 
$this->assertEqual($this->Users->Session->read('Auth.User.id'), 1); 
} 

Alternatif: En testLogin durumunda size kontrolörün aksiyon ve url ayarlamanız gerekir.