2017-12-12 1 views
1

주일부터 phpunit을 배우고 있습니다. 테스트 된 클래스에서 하나의 메소드 만 조롱하는 방법을 모르겠습니다. (예를 들어서 만 네임 스페이스를 쓰지 않았습니다). 어쩌면 당신은 나에게Phpunit 모의 테스트 클래스에서 단 하나의 방법 - 모의를 사용하여

class SomeService 
{ 
    public function firstMethod() 
    { 
     return 'smth'; 
    } 
    public function secondMethd() 
    { 
     return $this->firstMethod() . ' plus some text example'; 
    } 
} 

및 테스트 도움이 될 수 있습니다

class SomeServiceUnitTest extends TestCase 
{ 
    private $someService; 

    public function setUp() 
    { 
     parent::setUp(); 
     $this->someService = new SomeService(); 
    } 

    public function tearDown() 
    { 
     $this->someService = null; 
     parent::tearDown(); 
    } 

    public function test_secondMethod() 
    { 
     $mock = Mockery::mock('App\Services\SomeService'); 
     $mock->shouldReceive('firstMethod')->andReturn('rerg'); 
     exit($this->walletService->secondMethd()); 
    } 
} 

답변

1

당신은 당신의 테스트 클래스에 예와 같이 partial mocks을 사용할 수 있습니다, 당신은 할 수 있습니다 :

public function test_secondMethod() 
{ 
    $mock = Mockery::mock('App\Services\SomeService')->makePartial(); 
    $mock->shouldReceive('firstMethod')->andReturn('rerg'); 
    $this->assertEquals('rerg plus some text example', $mock->secondMethd()); 
} 

희망이 도움을

관련 문제