1

我想在 Angular 中创建消费者 Pact 测试。

我已将协议依赖项放入 package.json 中:

   "@pact-foundation/karma-pact": "2.1.9",
   "@pact-foundation/pact-node": "6.21.3",
   "@pact-foundation/pact-web": "7.2.0",

然后我更新了我的 karma.conf.js:

module.exports = function (config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular', 'pact'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular-devkit/build-angular/plugins/karma'),
            require('@pact-foundation/karma-pact')
        ],
        client: {
            clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(__dirname, '../coverage'),
            reports: ['html', 'lcovonly'],
            fixWebpackSourcePaths: true
        },
        angularCli: {
            environment: 'prod'
        },
        // load pact web module
        files: [
            '../node_modules/@pact-foundation/pact-web/pact-web.js'
        ],
        pact: [{
            cors: true,
            port: 1235,
            consumer: "ui",
            provider: "testservice",
            dir: "pacts",
            spec: 2
        }],
        proxies: {
            '/test-service/': 'http://127.0.0.1:1235/test-service/'
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_DEBUG,
        autoWatch: true,
        browsers: ['ChromeHeadless'],
        singleRun: true
    });
};

现在,当我开始 ng test 时,Karma 尝试启动 Pact 服务器并抛出错误:

07 02 2019 16:14:23.694:DEBUG [plugin]: Loading inlined plugin (defining framework:pact).
 10% building modules 2/2 modules 0 active[2019-02-07T15:14:23.890Z]  INFO: pact-node@6.21.3/16304 on VAA6DAB0246:
    Creating Pact Server with options: 
    cors = true,
    port = 1235,
    consumer = ui,
    provider = testservice,
    dir = D:\Users\aster\WebstormProjects\test\pacts,
    spec = 2,
    pactFileWriteMode = overwrite,
    ssl = false,
    host = localhost
[2019-02-07T15:14:23.905Z]  INFO: pact-node@6.21.3/16304 on VAA6DAB0246: Created 'standalone\win32-1.63.0\bin\pact-mock-service.bat service --cors 'true' --port '1235' --consumer 'ui' --provider 'testservice' --pact_dir 'D:\Users\aster\WebstormProjects\test\pacts' --pact_specification_version '2' --pact-file-write-mode 'overwrite' --host 'localhost'' process with PID: 35496
[2019-02-07T15:14:31.221Z] ERROR: pact-node@6.21.3/16304 on VAA6DAB0246:
    Pact Binary Error: Das System kann den angegebenen Pfad nicht finden.

[2019-02-07T15:14:31.482Z]  WARN: pact-node@6.21.3/16304 on VAA6DAB0246: Pact exited with code 1.
[2019-02-07T15:14:31.482Z]  INFO: pact-node@6.21.3/16304 on VAA6DAB0246: Removing Pact with PID: 35496
FEHLER: Der Prozess "35496" wurde nicht gefunden.                                                                                                                                                     07 02 2019 16:14:53.913:ERROR [pact]: Error while trying to run karma-pact: Error: Couldn't start Pact with PID: 35496

有谁知道,如何解决这个错误?或者也许有想法如何找出缺少的路径?

4

3 回答 3

0

我找到了这个错误的原因——我们的企业防火墙阻止了二进制文件的下载。我已经手动下载了二进制文件,现在 pact 可以正常工作。

于 2019-02-12T14:49:36.670 回答
0

我也遇到了这个问题(使用 Mac OS),在 hosts 文件中添加我的主机名作为别名解决了这个问题。

您可以通过在终端中运行来找到您的主机名。hostname它将类似于mymacbook.local.

然后在文件中添加/编辑这些条目,/etc/hosts使它们看起来像这样:

127.0.0.1   localhost mymacbook.local
::1         localhost mymacbook.local
于 2019-09-11T10:50:55.913 回答
0

我也遇到了同样的问题。正如 Ilja 的回答中所述,必须下载二进制文件。还必须在 package.json 中配置“pact_binary_location”。这在https://www.npmjs.com/package/@pact-foundation/pact-node上有很好的描述

于 2021-09-16T09:45:48.603 回答