您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit按照参数映射断言返回值-returnValueMap
发布时间:2021-09-22 15:03:44编辑:雪饮阅读()
就说有时候需要比如我传递a,b,c这样三个参数,那么最后返回值是d。
那么如果我传递1,2,3那么最后返回值是4.
就说是我可能有不同组参数,则返回值不同。
像是这种场景就可以用returnValueMap实现:
DatabaseTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class DatabaseTest extends TestCase
{
public function testReturnValueMapStub(): void
{
// 为类创建桩件。
$stub = $this->createStub(Database::class);
// Create a map of arguments to return values.
$map = [
['a', 'b', 'c','d'],
['e', 'f', 'g', 'h']
];
// 配置桩件。
$stub->method('doSomething')
->will($this->returnValueMap($map));
// $stub->doSomething() 根据提供的参数返回不同的值。
$this->assertSame('d', $stub->doSomething('a', 'b', 'c'));
$this->assertSame('h', $stub->doSomething('e', 'f', 'g'));
}
}
use PHPUnit\Framework\TestCase;
final class DatabaseTest extends TestCase
{
public function testReturnValueMapStub(): void
{
// 为类创建桩件。
$stub = $this->createStub(Database::class);
// Create a map of arguments to return values.
$map = [
['a', 'b', 'c','d'],
['e', 'f', 'g', 'h']
];
// 配置桩件。
$stub->method('doSomething')
->will($this->returnValueMap($map));
// $stub->doSomething() 根据提供的参数返回不同的值。
$this->assertSame('d', $stub->doSomething('a', 'b', 'c'));
$this->assertSame('h', $stub->doSomething('e', 'f', 'g'));
}
}
C:\Users\Administrator\PhpstormProjects\untitled\organizing>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe D:\phpstudy_pro\Extensions\php\php7.3.4nts\phpunit-9.5.8.phar C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\DatabaseTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.005, Memory: 20.00 MB
OK (1 test, 2 assertions)
关键字词:phpunit,returnValueMap