mcustiel/codeception-http-mock

v0.7 2017-01-25 22:18 UTC

This package is auto-updated.

Last update: 2024-09-19 09:18:49 UTC


README

此 Codeception 扩展允许开发人员和测试人员在使用 codeception 测试时使用 HttpMock 模拟外部服务。

codeception-http-mock 在测试运行之前启动 http-mock 实例,以便可以模拟外部服务。测试完成后,它将关闭连接并关闭 http-mock。

另请参阅

注意

http-mock 是一个不错用的应用程序,但它非常简单,缺乏很多功能。如果您需要一个更完整的应用程序来模拟和存根远程服务,请尝试 Phiremock。 Phiremock 也有一个codeception 扩展

安装

Composer

此项目已在 packagist 上发布,因此您只需将其添加到 composer.json 中的依赖项

    "require": {
        // ...
        "mcustiel/codeception-http-mock": "*"
    }

如果您想直接访问此存储库,将此内容添加到 composer.json 中应该足够

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/mcustiel/codeception-http-mock"
        }
    ],
    "require": {
        "mcustiel/codeception-http-mock": "dev-master"
    }
}

或者直接下载发行版并将其包含在您的路径中。

配置示例

扩展

# codeception.yml
extensions:
    enabled:
        - Codeception\Extension\HttpMock
    config:
        Codeception\Extension\HttpMock:
            port: 18080 # defaults to http-mock default port
            host: name.for.my.server # defaults to http-mock default host

模块

# acceptance.yml
modules:
    enabled:
        - HttpMock

如何使用

准备您的应用程序

首先,配置您的应用程序,以便在测试时用 http-mock 替换其外部服务。例如,如果您向位于 http://your.rest.interface 的 REST 服务发送一些请求,请将配置中的该 URL 替换为您在 http-mock 扩展配置中设置的 host。

编写您的测试

// YourCest.php
class YourCest extends \Codeception\TestCase\Test
{
    // tests
    public function tryToTest(\AcceptanceTester $I)
    {
        $I->expectRequest()->when()
                ->methodIs('GET')
                ->pathIs('/foo')
            ->then()
                ->body('mocked body')
            ->end();
        $I->doNotExpectAnyOtherRequest();
        $response = file_get_contents('http://localhost:28080/foo');
        $I->assertEquals('mocked body', $response);
    }
}