gpt4 book ai didi

javascript - 运行 npm run pactTest 时 PACT.io : Getting Missing requests error,

转载 作者:行者123 更新时间:2023-11-28 21:07:31 25 4
gpt4 key购买 nike

测试 repo 已创建:https://github.com/leongaban/pact-io-js-test

enter image description here

预计

运行 npm run pactTest 这将为我的 TotalPayout.test.pact.ts 文件创建一个 Pact 文件。

结果

D, [#38238] DEBUG -- : {
"description": "a GET request with a user id",
"request": {
"method": "GET",
"path": "/frontoffice/api/liquidity-pool/get-total-payout",
"headers": {
"Accept": "application/json"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
}
}
}
W, [#38238] WARN -- : Verifying - actual interactions do not match expected interactions.
Missing requests:
GET /frontoffice/api/liquidity-pool/get-total-payout


W, [#38238] WARN -- : Missing requests:
GET /frontoffice/api/liquidity-pool/get-total-payout

这是我的契约文件

// @ts-ignore
import path from 'path';
// @ts-ignore
import { Pact } from '@pact-foundation/pact';
import { getTotalPayout } from './LiquidityPool';

// const port = 12345;
const endpoint = '/frontoffice/api/liquidity-pool/get-total-payout';

const EXPECTED_BODY = {
total_payout: 100.21,
};

const userId = 'foo';

describe('The API', () => {
// Copy this block once per interaction under test
describe('getUsersTotalPayout', () => {
beforeEach(() => {
const interaction = {
uponReceiving: 'a GET request with a user id',
withRequest: {
method: 'GET',
path: endpoint,
headers: {
Accept: 'application/json',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json'
},
data: EXPECTED_BODY
},
};

// @ts-ignore
return provider.addInteraction(interaction);
});

// add expectations
it('Should call getUsersTotalPayout and return an object with the total_payout', done => {
getTotalPayout(userId)
.then((response: any) => {
console.log('response', response);
console.log('EXPECTED_BODY', EXPECTED_BODY);
expect(response).toEqual(EXPECTED_BODY);
})
.then(done);
});
});
});

这是包含 getTotalPayout 函数的服务文件:

这个端点还不存在,但我的理解是这个 Pact 测试应该仍然有效。

// @TODO Note, this is the placeholder for LiquidityPool API endpoints
// @ts-ignore
import axios, * as others from 'axios';

const endpoint = '/frontoffice/api/liquidity-pool/';

export const getTotalPayout = async (userId: string) => {
const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
return response.data;
};

还有我的 axios mock in src/__mocks__/axios.ts

// tslint:disable-next-line:no-empty
const mockNoop = () => new Promise(() => {});

export default {
get: jest.fn(() => Promise.resolve({ data: { total_payout: 100.21 }})),
default: mockNoop,
post: mockNoop,
put: mockNoop,
delete: mockNoop,
patch: mockNoop
};

最佳答案

很简单 - 您的测试没有在 Pact 模拟服务器上命中路径 /frontoffice/api/liquidity-pool/get-total-payout

您已将 Pact 设置为在 http://localhost:1234 上运行,因此您的实际代码需要配置为访问此服务器,而不是真正的服务器。

在您的 axios 配置中,您正在模拟 http 请求库,因此它什么都不做。因此,当您的真实代码使用它时,它不会进行 http 调用并且您的 Pact 测试会失败,因为它期待具有特定形状的调用但没有得到它。

以下是您需要更改的内容:

  1. 似乎使用了 Axios,但不在您的 package.json 中。我猜是因为你在 mock 它 (2)。你需要一个实际的 http 请求库,所以你应该只安装 axios
  2. Axios 被模拟以提供固定响应,在 Pact 测试期间不要模拟 axios(如果您愿意,本地开发人员可以这样做),因为这将阻止对 Pact 进行真正的调用。 Pact 需要对它进行真正的 http 调用,以便它可以检查您的代码是否在做正确的事情,然后将它发现的内容写入“契约(Contract)”
  3. 在测试中配置 axios 以访问 Pact 模拟服务。假设您使用 axios,应该执行类似以下的操作:

pactSetup.ts:

  // Configure axios to use the Pact mock server for Pact tests
import axios from "axios";
axios.defaults.baseURL = "http://localhost:1234";
  1. 阅读 Pact 输出的日志文件。它告诉你发生了什么。重申一下:您已经告诉 Pact 应该在 /frontoffice/api/liquidity-pool/get-total-payout 上进行交互,但它从未收到过。让您的实际代码达到此目的,然后就可以了。

最后,顺便说一下,一旦你开始生成 Pact(在 1-4 被修复之后),你可能想使用 Pact 作为本地 stub 服务器进行本地开发。该二进制文件实际上已经安装在您的 node_modules 中,有关其操作方式的文档位于 https://github.com/pact-foundation/pact-ruby-standalone/releases

我通常在 package.json 中有一个看起来像这样的脚本:

  "stubs": "$(find . -name pact-stub-service | head -n 1) pacts/* --port 4000"

然后您可以运行 npm run stubs 并让提供程序的本地 stub 在端口 4000 上运行,其中包含您放入测试中的所有请求/响应。

关于javascript - 运行 npm run pactTest 时 PACT.io : Getting Missing requests error,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52520827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com