2016-10-11 66 views
0

我對Laravel單元測試有點新。我需要通過調用相同的單元測試回購函數來獲得不同的輸出。Laravel單元測試兩次相同的函數和不同的輸出

到目前爲止,我的測試是這樣的:

public function testReportOffdayWorked() 
{ 
    $input = [ 
     'from_date' => '2016/01/01', 
     'to_date' => '2016/01/03', 
    ]; 

    $webServiceRepositoryMock = Mockery::mock('App\Repositories\WebServiceRepository'); 
    $webServiceRepositoryMock->shouldReceive('callGet')->twice()->andReturn($this->issues); 
    $this->app->instance('App\Repositories\WebServiceRepository', $webServiceRepositoryMock); 

    $this->call('post', '/reporting/portal/report-offdays', $input); 
    $this->assertResponseOk(); 
    $this->assertTrue($this->response->original->getName() == "Reporting::report_offday_worked"); 
} 

我想獲得兩個不同的輸出爲callGet功能。

回答

0

callGet()設置返回值或閉包的序列。

andReturn(value1, value2, ...)

設置返回值或關閉的順序。例如,第一次調用將返回值1和第二個值2。請注意,對模擬方法的所有後續調用將始終返回賦予此聲明的最終值(或唯一值)。

docs.mockery

下面介紹如何做到這一點PHPUnit中嘲笑和諷刺。

<?php 

class The { 
    public function answer() { } 
} 

class MockingTest extends \PHPUnit_Framework_TestCase 
{ 
    public function testMockConsecutiveCalls() 
    { 
     $mock = $this->getMock('The'); 
     $mock->expects($this->exactly(2)) 
      ->method('answer') 
      ->will($this->onConsecutiveCalls(4, 2)); 

     $this->assertSame(4, $mock->answer()); 
     $this->assertSame(2, $mock->answer()); 
    } 

    public function testMockeryConsecutiveCalls() 
    { 
     $mock = Mockery::mock('The'); 
     $mock->shouldReceive('answer')->andReturn(4, 2); 

     $this->assertSame(4, $mock->answer()); 
     $this->assertSame(2, $mock->answer()); 
    } 
} 
0

如何使用PHPUnit模擬框架?

$mock = $this->getMock('ClassName'); 

$mock->expects($this->at(0)) 
    ->method('getInt') 
    ->will($this->returnValue('one')); 

$mock->expects($this->at(1)) 
    ->method('getInt') 
    ->will($this->returnValue('two')); 

echo $mock->getInt(); //will return one 
echo $mock->getInt(); //will return two 
+0

正是我想要這樣的事情與laravel Mock對象。無論如何感謝您的回答:) – Lasith

相關問題