2016-10-11 67 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

相关问题