2013-01-04 7 views
3

컨트롤러 작업에 대해 작성하고 테스트하려고합니다. 여기에 내가 무엇을 가지고 :Cakephp 1.3 모의 인증

App::import('Component', 'Auth'); 
App::import('Controller', 'Goals'); 

class GoalControllerTest extends CakeTestCase { 

    function startCase() { 
      echo '<h1>Starting Test Case</h1>'; 
      $this->Goals = new TestGoalsController(); 
      $this->Goals->constructClasses(); 
      $this->Goals->Component->initialize($this->Goals); 
    } 

    function endCase() { 
      unset($this->Goals); 
      ClassRegistry::flush(); 
      echo '<h1>Ending Test Case</h1>'; 
    } 

    function startTest($method) { 
      echo '<h3>Starting method ' . $method . '</h3>'; 
      //$this->GoalsController = new TestGoalsController(); 
      Mock::generate('AuthComponent'); 
      $this->Goals->Auth = new MockAuthComponent(); 
    } 

    function endTest($method) { 
      echo '<hr />'; 
    } 

    function testSetHomepage() { 
      //get goal for test 
      $sql = "SELECT id FROM goals limit 1"; 
      $goal = $this->Goals->Goal->query($sql); 
      $this->Goals->params = Router::parse('/goals/setHomepage/'); 
      $this->Goals->beforeFilter(); 
      $this->Goals->Component->startup($this->Goals); 
      $this->Goals->params['url']['goal_id'] = $goal[0]['goals']['id']; 
      $this->Goals->params['url']['set_to'] = 1; 
      $this->Goals->setHomepage(); 

      //Mock Auth 
      $this->Goals->Auth->setReturnValue('user', 1); 


      //check the set 
      $sql = "SELECT GoalOnHome.goal_id FROM goals_users as GoalOnHome WHERE GoalOnHome.goal_id = '" . $this->Goals->params['url']['goal_id'] . "' limit 1"; 
      $result = $this->Goals->Goal->query($sql); 

      $expected = false; 
      $this->assertEqual(empty($result), $expected); 
      unset($this->Goals->params['url']); 
    } 
} 

내가 인증 구성 요소를 조롱하려고 내가 테스트를 실행할 때이 오류를 얻을 :

Unexpected PHP error [Undefined property: MockAuthComponent::$enabled] severity [E_NOTICE] in

누군가가 내가 뭐하는 거지를 도와 줄 수 여기서 뭐라구?

+0

I가 모의 genereate를 이동하여 그 정의되지 않은 속성 오류를 제거있어 : 기능 startCase를() { 에코 '

는 테스트 케이스

시작'; $ this-> Goals = new TestGoalsController(); Mock :: generate ('AuthComponent'); $ this-> Goals-> Auth = new MockAuthComponent(); $ this-> Goals-> constructClasses(); $ this-> 목표 -> 구성 요소 -> 초기화 ($ this-> 목표); }하지만이 오류가 발생하는 것을 알고 : 치명적인 오류 : 정의되지 않은 메서드 AuthComponent :: setReturnValue() 호출 – rstewart

답변

0

나는 최근에 같은 문제가있었습니다. Mock 객체에 속성을 설정하기 만하면됩니다. 또한 startup -method에 대한 반환 값을 구성했습니다. 그렇지 않으면 조롱 된 인증이 작동하지 않습니다.

function startTest() { 
    // Set up controller etc. 
    // ... 
    $this->Goals->Auth = new MockAuthComponent(); 
    $this->Goals->Auth->enabled = true; // Should solve your error 
    $this->Goals->Auth->setReturnValue('startup', true); 
}