Php 嘲讽——如何正确嘲讽
Php Mockery - How To Mock Correctly
我正在尝试将一些现有的测试用例添加到现有的项目中
这里是 API class
<?php
namespace MyApp\Api;
use MyApp\ApiBase;
use MyApp\Ethereum as Eth;
use PHPUnit\Runner\Exception;
/**
* Class Ethereum
* @package MyApp\Api
*/
class Ethereum extends ApiBase {
/**
* Function to get balances
* @param array $addresses
* @param string $tag
* @return array
*/
public function getBalances(array $addresses, string $tag = 'latest') {
$data = [];
foreach ($addresses as $addr) {
// validate address
if (!Eth::validateAddress($addr)) {
continue;
}
}
return $data;
}
}
服务class
<?php
namespace MyApp;
use MyApp\Ethereum\GethIpc;
use MyApp\Ethereum\GethWebsocket;
use PHPUnit\Runner\Exception;
/**
* Class Ethereum
* @package MyApp
*/
class Ethereum {
public static $subscriptions = [];
/**
* Ethereum constructor.
*/
public function __construct() {
$this->connection = new GethWebsocket();
$connect = $this->connection->connect();
}
/**
* Function to validate an address
* @param string $address
* @return bool
*/
public static function validateAddress(string $address) {
return preg_match("/^(0x)?[0-9a-fA-F]{40}$/", $address) !== 0;
}
}
我的测试class
<?php
declare(strict_types=1);
namespace MyApp\Test;
use MyApp\Api\Ethereum;
use MyApp\Ethereum as Eth;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Exception;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
* @covers MyApp\Api\Ethereum
*/
final class EthereumTest extends MockeryTestCase {
protected $ethereumApi;
protected $ethereum_address;
//Setup method called before every method
protected function setUp(): void {
$this->ethereumApi = new Ethereum();
$this->ethereum_address = '0x0000000000000000000000000000000' . rand(100000000, 999999999);
//Mockery::globalHelpers();
//$mock = mock(MyApp\Ethereum::class);
}
public function testGetBalances_ValidEthereumAddress(): void {
$mockEthereumService = Mockery::mock("Eth");
$mockEthereumService->shouldReceive('validateAddress')->once()->with($this->ethereum_address)->andReturn(true);
//$mockEthereumService->shouldReceive('msg')->once()->with($this->ethereum_address)->andReturn(true);
$addresses = [$this->ethereum_address];
$result = $this->ethereumApi->getBalances($addresses);
$this->assertNotEmpty($result);
}
public function tearDown()
{
Mockery::close();
}
}
每次我 运行 测试 class - 模拟不工作并且正在调用实际服务 class 方法
任何人都可以帮助我如何让这个模拟示例正常工作吗?
我自己没有使用 Mockery 的经验,但是在查看了文档之后
- http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#overloading
- http://docs.mockery.io/en/latest/cookbook/mocking_hard_dependencies.html
我假设您需要使用 "overload" 前缀和完整的类名,并从您的测试用例中删除 MyApp\Ethereum 的使用语句。
我正在尝试将一些现有的测试用例添加到现有的项目中 这里是 API class
<?php
namespace MyApp\Api;
use MyApp\ApiBase;
use MyApp\Ethereum as Eth;
use PHPUnit\Runner\Exception;
/**
* Class Ethereum
* @package MyApp\Api
*/
class Ethereum extends ApiBase {
/**
* Function to get balances
* @param array $addresses
* @param string $tag
* @return array
*/
public function getBalances(array $addresses, string $tag = 'latest') {
$data = [];
foreach ($addresses as $addr) {
// validate address
if (!Eth::validateAddress($addr)) {
continue;
}
}
return $data;
}
}
服务class
<?php
namespace MyApp;
use MyApp\Ethereum\GethIpc;
use MyApp\Ethereum\GethWebsocket;
use PHPUnit\Runner\Exception;
/**
* Class Ethereum
* @package MyApp
*/
class Ethereum {
public static $subscriptions = [];
/**
* Ethereum constructor.
*/
public function __construct() {
$this->connection = new GethWebsocket();
$connect = $this->connection->connect();
}
/**
* Function to validate an address
* @param string $address
* @return bool
*/
public static function validateAddress(string $address) {
return preg_match("/^(0x)?[0-9a-fA-F]{40}$/", $address) !== 0;
}
}
我的测试class
<?php
declare(strict_types=1);
namespace MyApp\Test;
use MyApp\Api\Ethereum;
use MyApp\Ethereum as Eth;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Exception;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
/**
* @covers MyApp\Api\Ethereum
*/
final class EthereumTest extends MockeryTestCase {
protected $ethereumApi;
protected $ethereum_address;
//Setup method called before every method
protected function setUp(): void {
$this->ethereumApi = new Ethereum();
$this->ethereum_address = '0x0000000000000000000000000000000' . rand(100000000, 999999999);
//Mockery::globalHelpers();
//$mock = mock(MyApp\Ethereum::class);
}
public function testGetBalances_ValidEthereumAddress(): void {
$mockEthereumService = Mockery::mock("Eth");
$mockEthereumService->shouldReceive('validateAddress')->once()->with($this->ethereum_address)->andReturn(true);
//$mockEthereumService->shouldReceive('msg')->once()->with($this->ethereum_address)->andReturn(true);
$addresses = [$this->ethereum_address];
$result = $this->ethereumApi->getBalances($addresses);
$this->assertNotEmpty($result);
}
public function tearDown()
{
Mockery::close();
}
}
每次我 运行 测试 class - 模拟不工作并且正在调用实际服务 class 方法
任何人都可以帮助我如何让这个模拟示例正常工作吗?
我自己没有使用 Mockery 的经验,但是在查看了文档之后
- http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#overloading
- http://docs.mockery.io/en/latest/cookbook/mocking_hard_dependencies.html
我假设您需要使用 "overload" 前缀和完整的类名,并从您的测试用例中删除 MyApp\Ethereum 的使用语句。