Bol*_*lek 5 php phpunit unit-testing mockery
我从一周开始学习 phpunit。我不知道如何只模拟测试类中的一种方法。(这只是示例,所以我没有写命名空间)。也许你可以帮助我
class SomeService
{
public function firstMethod()
{
return 'smth';
}
public function secondMethd()
{
return $this->firstMethod() . ' plus some text example';
}
}
Run Code Online (Sandbox Code Playgroud)
并测试:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
You can use a partial mocks, as example on your test class, you can do:
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());
}
Run Code Online (Sandbox Code Playgroud)
Hope this help
| 归档时间: |
|
| 查看次数: |
6132 次 |
| 最近记录: |