API与Pact和Angular的合同测试



我有这个函数:

saveApplication(details: Application): Observable<any> {
return this.http.post(`http://localhost:5000/Apply`, details);
}

我正在为它写一个契约测试(API合同测试(,下面是测试:

describe('ApplicationService contract testing', () => {
let provider: PactWeb;
let applicationService: ApplicationService;
beforeAll(function (done) {
provider = new PactWeb({
cors: true, host: '127.0.0.1', port: 9776, logLevel: 'error'
});
setTimeout(done, 2000);
provider.removeInteractions();
});
afterAll(function (done) {
provider.finalize().then(function () {
done();
}, function (err) { done.fail(err); });
});
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ApplicationService],
imports: [HttpClientTestingModule]
});
applicationService = getTestBed().get(ApplicationService);
});
afterEach((done) => {
provider.verify().then(done, e => done.fail(e));
});
describe('Save', () => {
const _details: Application = {
title: 'Mr', firstName: 'Application', lastName: 'applicant',
dob: new Date(1977, 10, 10).toString(), gender: 'M'
};
beforeAll((done) => {
provider.addInteraction({
state: ' a task to save the dashboard application to mongo',
uponReceiving: ' a request to post',
withRequest: {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
path: '/Apply', body: _details
}, willRespondWith:
{
status: 201,
headers: {
'Content-Type': 'application/json'
}
}
}).then(done, e => done.fail(e));
it('should call the API to Post the application Content', (done) => {
const _response: any = {};
applicationService.saveApplication(_details).subscribe(res => {
expect(res).toEqual(_response);
done();
}, error => { done.fail(error); }
);
});
});
});
});

当id npm运行测试或ng测试时,测试没有运行,但正在创建pacts目录。我不知道我的配置缺少什么

这就是我在因果报应中所拥有的:

frameworks: ['jasmine', '@angular-devkit/build-angular','pact'],
plugins: [....,
require('@pact-foundation/karma-pact')],
pact: [{
cors: true,
port: 9776,
consumer: "ui",
provider: "Apply",
dir: "pacts/",
spec: 2
}],
proxies: {
'/api/applications/v1/': 'http://localhost:9776/Apply/'
},

不确定我错过了什么。有什么帮助吗?

如果您的测试根本没有运行,但正在生成pact文件夹,这意味着:

  1. 因果报应协定插件正在启动
  2. 有一些问题根本无法运行测试

我会从你的测试中删除所有的约定内容开始,直到你可以进行绿色测试(这什么都不做(,然后一个接一个地添加内容。

我不太清楚代理发生了什么,但它似乎与代码的实际操作不一致(代理路径是/api/applications/v1,它似乎将它发送到端口9776上的Pact,但您的代码直接命中路径/Apply(。

我不是角度专家,所以为了防止不清楚,您需要确保saveApplication函数在测试中调用实际的pact mock服务。

旁注:done的使用非常古老,在大多数情况下,您应该能够翻译:

afterAll(function (done) {
provider.finalize().then(function () {
done();
}, function (err) { done.fail(err); });
});

至:

afterAll(() => provider.finalize())

最新更新