【问题标题】:Php Mockery - How To Mock CorrectlyPhp Mockery - 如何正确模拟
【发布时间】:2018-07-02 12:11:54
【问题描述】:

我正在尝试将一些现有的测试用例添加到预先存在的项目中 这是API类

    <?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;
    }

}

服务类

<?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;
    }


}

我的测试课

<?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();
    }
}

每次我运行测试类时 - 模拟不工作并且正在调用实际的服务类方法

任何人都可以就我应该如何让这个模拟示例正常工作提供帮助吗?

【问题讨论】:

    标签: php unit-testing mockery


    【解决方案1】:

    我自己没有使用 Mockery 的经验,但是在查看了文档之后

    1. http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#overloading
    2. http://docs.mockery.io/en/latest/cookbook/mocking_hard_dependencies.html

    我假设您需要使用“overload”前缀和完整的类名,并从测试用例中删除 MyApp\Ethereum 的使用语句。

    【讨论】:

    • 我不需要 MyApp\Ethereum 语句吗?服务类和api类都叫以太坊,但在不同的包中
    猜你喜欢
    • 2019-10-09
    • 2020-05-10
    • 2013-09-30
    • 2013-11-04
    • 2015-08-15
    • 2013-07-29
    • 2018-03-26
    • 2015-03-27
    • 2013-10-29
    相关资源
    最近更新 更多