2013-05-01 2 views
0

PHPUnit을 이용한 단위 테스트를 배우고 있으며 해결할 수없는 mock 객체와 관련된 이상한 문제가 있습니다.PHPUnit 모의 객체 메쏘드

클래스 1

class PrimaryObj1 
{ 
    function doNothing(SecondObj $s) 
    { 
    } 
} 

Class2의

class PrimaryObj2 
{ 
    function doNothing(SecondObj $s) 
    { 
     $s->TestMethod(); 
    } 
} 

및 테스트 파일로 :

class PrimaryObj1_test extends PHPUnit_Framework_TestCase 
{ 
    private $primaryObj; 
    function setUp() 
    { 
     $this->primaryObj = new PrimaryObj1(); 
    } 
    function testDoNothing() 
    { 
     $secondObj = $this->getMock('SecondObj'); 
     $secondObj->expects($this->once()) 
        ->method("TestMethod"); 
     $this->primaryObj->doNothing($secondObj); 
    } 
} 

(한 내가 경험하고있는 무슨의 예 이러한 더미 파일을 설정 클래스 이름을 제외하고 모든 것이 같은 더미 클래스 각각에 대한 테스트 파일).

Running Tests/PrimaryObj1_test.php 
1) PrimaryObj1_test::testDoNothing 
Expectation failed for method name is equal to <string:TestMethod> when invoked 1 time(s). 
Method was expected to be called 1 times, actually called 0 times. 

Running Tests/PrimaryObj2_test.php 
Fatal error: Call to undefined method Mock_SecondObj_99c898e7::TestMethod() in PrimaryObj2.php on line 5 

을 그래서 일단 예상되는 메소드를 호출하지 않았다 화가하지만 그렇게되면 다음 그것이 정의되지 않은 원인 미친 가져옵니다 내가 phpunit을 실행하면

, 이것은 내가 무엇을 얻을 수 있습니다. 나는 단지 이길 수 없다. 나는 이것이 내가 왜 아직도 싱글 인 이유라고 생각한다.

왜 이런 일이 일어날 지 생각하고 있니?

+0

'SecondObj'의 코드 표시 – hek2mgl

+0

SecondObj는 Mock으로 존재합니다. 코드 또는 클래스 선언이 없습니다 – baiano

+0

AFAIK 조롱 된 클래스가 있어야합니다. – hek2mgl

답변

0

답변과 함께 이메일 목록에서 답변을 받았습니다. 이 라인 :

$secondObj = $this->getMock('SecondObj'); 

은 다음과 같아야합니다

$secondObj = $this->getMock('SecondObj', array('TestMethod')); 

내가 예상대로 일이 변경 한 후.