我正在为大学项目编写一个小型程序。 我想用契约框架来测试它。不幸的是,尽管没有错误,但没有为我创建Pact.json文件。我的消费者用Java语言编写。 在下面,您可以看到我的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"
}
}
我非常感谢您的帮助,并在此先感谢
答案 0 :(得分:0)
我无法在这里没有日志,但是有一点可以肯定:您对axios.get
和provider.verify
的呼叫是承诺,并且没有正确执行,这意味着某些事情的执行将会失败或根本不执行。
只需在两个前缀return
之前都可以解决该问题。
请参见https://github.com/pact-foundation/pact-js#test-fails-when-it-should-pass。