问题:使用 Pact 进行消费者驱动的合同测试
Problem: Consumer Driven Contract Testing with Pact
我正在为一个大学项目编写一个小程序。
我想用 pact framewok 测试它。不幸的是,没有为我创建 Pact.json 文件,尽管没有错误。我的 Consumer iss 写在 Javascript 中。
您可以在下面看到我的 javascript 文件的源代码、控制台输出和我的 package.json 文件:
const {Pact} = require('@pact-foundation/pact');
const axios = require('axios');
const path = require('path');
describe('Pact Consumer', () => {
const provider = new Pact({
consumer: 'consumer',
provider: 'producer',
host:'127.0.0.1',
port: 1234,
log: path.resolve(process.cwd(), 'logs', 'pact.log'),
dir: path.resolve(process.cwd(), 'pacts'),
logLevel: 'INFO',
});
beforeAll(() => provider.setup());
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
describe('consumer', () => {
beforeEach
(() =>
provider.addInteraction({
state: "valid date",
uponReceiving: "a request for JSON data",
withRequest: {
method: "GET",
path: "/test",
headers: { Accept: "application/json" },
},
willRespondWith: {
status: 200,
headers: { "Content-Type": "application/json" },
body:
{
name: 'Scherr',
surname: 'Valerian',
age: 28,
},
},
}),
);
});
describe('test', () => {
it('should return the correct data', () => {
return axios.get('localhost:1234/test').then(response => {
expect(response[0].name).toBe('Scherr');
expect(response[0].surname).toBe('Valerian');
expect(response[0].age).toBe(28);
}).then(()=> provider.verify())
});
});
afterAll(() => {
return provider.finalize();
});
});
控制台输出:
Error: getaddrinfo ENOTFOUND 1234
Expected :
Actual :
<Click to see difference>
error properties: Object({ errno: 'ENOTFOUND', code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: '1234', config: Object({ url: 'localhost:1234/test', method: 'get', headers: Object({ Accept: 'application/json, text/plain, */*', User-Agent: 'axios/0.19.2' }), transformRequest: [ Function ], transformResponse: [ Function ], timeout: 0, adapter: Function, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: Function, data: undefined }), request: Writable({ _writableState: WritableState({ objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: false, ended: false, finished: false, destroyed: false, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: true, bufferProcessing: false, onwrite: Function, writecb: null, writelen: 0, afterWriteTickInfo: null, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 0, prefinished: false, errorEmitted: false, emitClose: true, autoDestroy: false ...
Error: getaddrinfo ENOTFOUND 1234
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26)
package.json:
{
"name": "webservice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jasmine"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@pact-foundation/pact": "^9.11.0",
"@pact-foundation/pact-node": "^10.9.5",
"axios": "^0.19.2",
"jasmine": "^3.5.0"
},
"devDependencies": {
"pact": "^4.3.2"
}
}
非常感谢任何帮助并提前致谢
这里没有日志我无法判断,但有一件事是肯定的:你对 axios.get
和 provider.verify
的调用是承诺,并且没有正确执行,这意味着某些事情的执行将出现故障或根本不会实际执行。
只需在两者前加上 return
前缀即可解决该问题。
参见https://github.com/pact-foundation/pact-js#test-fails-when-it-should-pass。
我遇到了类似的问题。将 provider config
上的 cors
属性 设置为 true
为我完成了工作。
我在使用 node-fetch
而不是 axios
时似乎没有这个问题。
我正在为一个大学项目编写一个小程序。 我想用 pact framewok 测试它。不幸的是,没有为我创建 Pact.json 文件,尽管没有错误。我的 Consumer iss 写在 Javascript 中。 您可以在下面看到我的 javascript 文件的源代码、控制台输出和我的 package.json 文件:
const {Pact} = require('@pact-foundation/pact');
const axios = require('axios');
const path = require('path');
describe('Pact Consumer', () => {
const provider = new Pact({
consumer: 'consumer',
provider: 'producer',
host:'127.0.0.1',
port: 1234,
log: path.resolve(process.cwd(), 'logs', 'pact.log'),
dir: path.resolve(process.cwd(), 'pacts'),
logLevel: 'INFO',
});
beforeAll(() => provider.setup());
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
describe('consumer', () => {
beforeEach
(() =>
provider.addInteraction({
state: "valid date",
uponReceiving: "a request for JSON data",
withRequest: {
method: "GET",
path: "/test",
headers: { Accept: "application/json" },
},
willRespondWith: {
status: 200,
headers: { "Content-Type": "application/json" },
body:
{
name: 'Scherr',
surname: 'Valerian',
age: 28,
},
},
}),
);
});
describe('test', () => {
it('should return the correct data', () => {
return axios.get('localhost:1234/test').then(response => {
expect(response[0].name).toBe('Scherr');
expect(response[0].surname).toBe('Valerian');
expect(response[0].age).toBe(28);
}).then(()=> provider.verify())
});
});
afterAll(() => {
return provider.finalize();
});
});
控制台输出:
Error: getaddrinfo ENOTFOUND 1234
Expected :
Actual :
<Click to see difference>
error properties: Object({ errno: 'ENOTFOUND', code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: '1234', config: Object({ url: 'localhost:1234/test', method: 'get', headers: Object({ Accept: 'application/json, text/plain, */*', User-Agent: 'axios/0.19.2' }), transformRequest: [ Function ], transformResponse: [ Function ], timeout: 0, adapter: Function, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, validateStatus: Function, data: undefined }), request: Writable({ _writableState: WritableState({ objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: false, ended: false, finished: false, destroyed: false, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: true, bufferProcessing: false, onwrite: Function, writecb: null, writelen: 0, afterWriteTickInfo: null, bufferedRequest: null, lastBufferedRequest: null, pendingcb: 0, prefinished: false, errorEmitted: false, emitClose: true, autoDestroy: false ...
Error: getaddrinfo ENOTFOUND 1234
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26)
package.json:
{
"name": "webservice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jasmine"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@pact-foundation/pact": "^9.11.0",
"@pact-foundation/pact-node": "^10.9.5",
"axios": "^0.19.2",
"jasmine": "^3.5.0"
},
"devDependencies": {
"pact": "^4.3.2"
}
}
非常感谢任何帮助并提前致谢
这里没有日志我无法判断,但有一件事是肯定的:你对 axios.get
和 provider.verify
的调用是承诺,并且没有正确执行,这意味着某些事情的执行将出现故障或根本不会实际执行。
只需在两者前加上 return
前缀即可解决该问题。
参见https://github.com/pact-foundation/pact-js#test-fails-when-it-should-pass。
我遇到了类似的问题。将 provider config
上的 cors
属性 设置为 true
为我完成了工作。
我在使用 node-fetch
而不是 axios
时似乎没有这个问题。