【发布时间】:2020-12-01 07:13:08
【问题描述】:
我有这个功能:
saveApplication(details: Application): Observable<any> {
return this.http.post(`http://localhost:5000/Apply`, details);
}
我正在为它编写一个 Pact 测试(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 run test 或 ng test 时,测试没有运行,ut 正在创建 pacts 目录。我不知道我的配置中缺少什么
这就是我在 Karma 中所拥有的:
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/'
},
不知道我错过了什么。有什么帮助吗?
【问题讨论】:
标签: angular unit-testing jasmine karma-jasmine pact