2012-09-20 1 views
1

PHPUnit 테스트에 문제가 있습니다.PHPUnit - 메소드가 호출 횟수에 의해 '실행'/ '호출'로 계산되는 경우는 언제입니까?

public function bla() 
{ 
    $this->blub(); 
} 

$ _testObject이 푸의 조롱 인스턴스 다음과 같이 :처럼 나는 방법이

public function setUp() 
{ 
    $this->_testObject = $this->getMockBuilder('Foo') 
           ->setMethods(array('blub')) 
           ->getMock(); 
} 

내 시험 방법은 같은 것입니다 :이 테스트의

/** 
* @test 
* @covers Foo::bla 
*/ 
public function shouldCallBlubOnce() 
{ 
    $this->_testObject->expects($this->once()) 
         ->method('blub'); 

    //forgot this one :D 
    $this->_testObject->bla(); 
} 

결과 제공 :

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

감사합니다. 광고 vance. :)

+0

mock 객체뿐만 아니라'bla' 호출하는 코드를 생성하는 코드를 게시하시기 바랍니다. –

+0

@David 죄송합니다. 예제 테스트 메소드에 'bla' 호출을 추가하는 것을 잊어 버렸습니다. 이제 구조가 실제 테스트의 구조와 비슷합니다. 추가 된 testObject 생성 예제가 도움이되기를 바랍니다. – beToiba

답변

0

다음 테스트는 PHPUnit 3.6.12를 사용하여 내 시스템으로 전달됩니다. 테스트를 실행

class Foo { 
    function bla() { 
     $this->blub(); 
    } 

    function blub() { 
     echo 'blub'; 
    } 
} 

class FooTest extends PHPUnit_Framework_TestCase { 
    function setUp() { 
     $this->_testObject = $this->getMockBuilder('Foo') 
            ->setMethods(array('blub')) 
            ->getMock(); 
    } 

    /** 
    * @test 
    * @covers Foo::bla 
    */ 
    public function shouldCallBlubOnce() 
    { 
     $this->_testObject->expects($this->once()) 
          ->method('blub'); 
     $this->_testObject->bla(); 
    } 
} 

이 생산 :

PHPUnit 3.6.12 by Sebastian Bergmann. 

. 

Time: 0 seconds, Memory: 12.50Mb 

OK (1 test, 1 assertion) 
+0

좋아, 3.6.12 (현재 3.6.10)로 업데이트하고 다시 시도하겠습니다. 잘하면이 문제가 해결 될 것입니다. :) – beToiba

+0

그래서 3.7.1로 업데이트되었고 작동합니다! 힌트를 보내 주셔서 감사합니다. @ David. – beToiba

관련 문제