[英]Mock Http get request for unit testing angular service
I want to know how I can test my service function that has a http get and post calls.我想知道如何测试具有 http get 和 post 调用的服务功能。 I could configure the spec file where I could create an instance of the service, also followed few sites for creating a mockhttp service, It says "httpMock.expectOne is not a function"
我可以配置规范文件,我可以在其中创建服务实例,也可以关注几个站点来创建 mockhttp 服务,它说“httpMock.expectOne 不是函数”
reference from here: https://alligator.io/angular/testing-http-interceptors/从这里参考: https : //alligator.io/angular/testing-http-interceptors/
describe('Service: UserSearchService', () => {
class AppLookupServiceStub {
//some functions
}
UserSearchService = new UserSearchService(httpMock,
appLookUpService);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
AppHttpService,
HttpClient,
AppCookieService,
CookieService,
{ provide: AppLookupService, useClass: AppLookupServiceStub },
AppConstants
]
});
});
beforeEach(async() => {
service = TestBed.get(UserSearchService);
httpMock = TestBed.get(AppHttpService);
selectedCriteria = [
//some data
]
UserSearchOptions = {
//some data
}
});
it('should have a service instance', () => {
expect(service).toBeDefined();
});
it('should make expected calls to getUserSerachOptions', inject(
[AppHttpService, UserSearchService],
(
httpMock: AppHttpService,
dataService: UserSearchService
) => {
// let spy = spyOn(service, 'getUserSerachOptions');
// UserSearchService.getUserSerachOptions();
// expect(spy).toBeDefined();
dataService.getUserSerachOptions().subscribe(UserSearchOptions => {
expect(UserSearchOptions).toEqual(UserSearchOptions);
});
const req = httpMock.expectOne(`AppConstants.ADMIN_CENTRAL_SELECTION_CRITERIA_URL`);
expect(req.request.method).toBe("GET");
req.flush(UserSearchOptions);
}));
it('should get the data successful', () => {
service.getUsersBySelectionCriteria(selectedCriteria[0]).subscribe((data: any) => {
console.log(data);
expect(data.country.name).toBe('Australia');
});
});
})
test spec for service:服务测试规范:
getUserSerachOptions(): Observable<UserSearchOptions> {
if (!this.userSearchCriteriaOptions) {
return this.appHttpService.get('{apiUrl}/xyzproject/{devKey}/userSelectionCriteria').pipe(map(
(data: UserSearchOptions) => {
this.userSearchCriteriaOptions = data;
return this.userSearchCriteriaOptions;
}
));
}
return of( this.userSearchCriteriaOptions);
}
How do I mock the get call, I am unable to find any sources in this case.我如何模拟 get 调用,在这种情况下我找不到任何来源。
i think that this is wrong我认为这是错误的
httpMock = TestBed.get(AppHttpService);
it should be它应该是
httpMock = TestBed.get(HttpTestingController);
httpMock is an HttpTestingController , try this httpMock 是一个HttpTestingController ,试试这个
import { HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
describe('Service: UserSearchService', () => {
let httpMock: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([]),
HttpClientTestingModule
],
declarations: [Component],
providers: [
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
fixture = TestBed.createComponent(Component);
component = fixture.componentInstance;
httpMock = injector.get(HttpTestingController); // change it here
}));
your test你的测试
it('should make expected calls to getUserSerachOptions', inject([HttpTestingController],
(httpTestMock: HttpTestingController) => {
httpMock.expectOne(...) // this should work now
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.