2017-12-12 4 views
1

私は週からphpunitを学んでいます。テストされたクラスから一つのメソッドだけをどうやって模倣するのか分かりません。 (それは私が名前空間を書いていないので例です)。たぶん、あなたは私にPhpunit模擬テストクラスの1つのメソッド - Mockeryを使用

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()); 
} 

・ホープこのヘルプを

関連する問題