Angular 如何对返回承诺的异步方法进行单元测试
我正在尝试使用在我的Angular应用程序中返回承诺的方法测试服务 但是我不断得到错误异步函数没有在5000ms内完成 我的服务是这样的Angular 如何对返回承诺的异步方法进行单元测试,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我正在尝试使用在我的Angular应用程序中返回承诺的方法测试服务 但是我不断得到错误异步函数没有在5000ms内完成 我的服务是这样的 public async startApp(data) { if (data === { }) this.logout() const url = environment.API_HOST + environment.ENDPOINT + '/start-app' return this.sendRequest<respons
public async startApp(data) {
if (data === { }) this.logout()
const url = environment.API_HOST + environment.ENDPOINT + '/start-app'
return this.sendRequest<responseType>(url, data).toPromise()
}
private sendRequest<T>(url: string, data: any): Observable<T> {
return this.http.post<T>(
url, data, { headers: this.headers(), observe: 'response', responseType: 'json' }
).pipe(
map((resp: any) => {
return resp.body
}))
}
我做错了什么
如果可能的话,我会避免监视
sendRequest
,因为它是一个私有方法,我无法测试它,否则看到你的应用正在执行http,你需要将它插入到你的配置中
import { HttpClientTestingModule,
HttpTestingController } from '@angular/common/http/testing';
......
describe('MyService', () => {
let httpTestingController: HttpTestingController;
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [MyService],
imports: [HttpClientTestingModule],
});
// get a handle on the HTTPController
httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(CoursesService);
});
afterEach(() => {
// verify no pending requests after each test
httpTestingController.verify();
});
it('should start app and send a post request', async (done: DoneFn) => {
service.startApp({ data: 'someData' }).then(response => {
expect(response.body).toBe('helloWorld');
// put the rest of your assertions here.
done();
});
const mockResponse = { body: 'helloWorld' }; // mock the response to what you would like
const req = httpTestingController.expectOne(... put here whatever the url, data is/ will resolve to);
expect(req.request.method).toEqual('POST');
// for the next HTTP request for that URL, give this as the response
req.flush(mockResponse);
});
});
startApp
调用一个函数,该函数以一种可观察的方式进行API调用,但您将其转换为承诺,但我非常确定我为您提供的应该很好
很好的链接:
import { HttpClientTestingModule,
HttpTestingController } from '@angular/common/http/testing';
......
describe('MyService', () => {
let httpTestingController: HttpTestingController;
let service: MyService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [MyService],
imports: [HttpClientTestingModule],
});
// get a handle on the HTTPController
httpTestingController = TestBed.get(HttpTestingController);
service = TestBed.get(CoursesService);
});
afterEach(() => {
// verify no pending requests after each test
httpTestingController.verify();
});
it('should start app and send a post request', async (done: DoneFn) => {
service.startApp({ data: 'someData' }).then(response => {
expect(response.body).toBe('helloWorld');
// put the rest of your assertions here.
done();
});
const mockResponse = { body: 'helloWorld' }; // mock the response to what you would like
const req = httpTestingController.expectOne(... put here whatever the url, data is/ will resolve to);
expect(req.request.method).toEqual('POST');
// for the next HTTP request for that URL, give this as the response
req.flush(mockResponse);
});
});