2012-02-04 4 views
2

CakePHP 모의 객체를 사용하여 세션 구성 요소에 값을 입력했으나 테스트에서 write 메소드가 호출되지 않았다고 계속 테스트하려고합니다. 나는 이것을 디버깅했고 메소드가 호출 되었기 때문에 내가 무엇을 잘못하고 있는지 알지 못한다. 나는 다음과 같은 오류 얻을CakePHP 유닛 테스트 모의 컨트롤러

$this->controller = $this->generate('Posts', array(
    'components' => array(
    'Session' => array('write') 
))); 

$this->testAction(...); 

$this->controller->Session 
    ->expects($this->any()) 
    ->method('write') 
    ->with('my value here'); 

: 여기

는 단위 테스트의 코드 내가 전화를 변경하는 경우

Expectation failed for method name is equal to when invoked zero or more times. Mocked method does not exist.

을하는 것으로 예상 -> 기대 ($ this-> 한 번()) 나는이 오류 얻을 :

Expectation failed for method name is equal to when invoked 1 time(s). Method was expected to be called 1 times, actually called 0 times.

을 나는 $ this-에 위해서 var_dump를했다> 컨트롤러, 그리고 확실히 조롱 세션 객체가, 그리고 그것은 'write'메소드에 대한 호출을 알아 차리는 것처럼 보입니다. 그래서 나는 왜 에러 메시지를 받는지 잘 모르겠습니다.

조언을 보내 주시면 감사하겠습니다.

+0

어떤 버전의 CakePHP를 사용하고 있습니까? – Farray

+0

CakePHP 2.0.4를 사용 중입니다. – rikkyc

답변

2

모의 셋업 코드에서와 같이 $this->testAction()에 호출하기 전에해야한다 :

$this->controller = $this->generate('Posts', array(
    'components' => array(
    'Session' => array('write') 
))); 

//Add the method mock details here 
$this->controller->Session 
    ->expects($this->any()) 
    ->method('write') 
    ->with('my value here'); 

//Then call testAction 
$this->testAction(...); 

문제를 해결해야한다고.

관련 문제