我正在尝试对Angular中的服务层进行单元测试,但是当我尝试模拟http获取,发布和放置的请求时,我收到一条错误消息,提示它找不到打开的请求,但找到了1 >
我已经尝试搜索多个与此问题相关的帖子,但似乎没有一个可以挽救我,但是我似乎最终无济于事
it('should be able load company data', inject([HttpClient], (http: HttpClient) => {
const companyList: CompanyVerificationResponseDto = {
companyName: 'testcompany',
kvkNumber: 12345678,
companyPhone: '0612341234',
email: 'testtest@email.com',
accountStatus: 0,
contactPhone: '0631313131'
};
managementService.loadData();
const headers = new HttpHeaders().set('Content-Type', 'application/json');
const param = new HttpParams().set('token', '1234-1234-1234').append('email', 'testtest@email.com');
const options = {params: param, headers: headers};
http.get('http://localhost:8080/adminpage', options).subscribe(() => {
});
const req = httpMock.expectOne('http://localhost:8080/adminpage?token=1234-1234-1234&email=testtest@email.com');
expect(req.request.method).toEqual('GET');
req.flush(companyList);
}));
afterEach(() => {
httpMock.verify();
});
我的实际服务方法如下:
public loadData() {
this.restfulService.url = this.restfulService.getBaseUrl() + '/adminpage';
const param = new HttpParams().set('token', this.restfulService.getLocalStorageToken().token);
const options = {params: param, headers: this.headers};
return this.httpClient.get<CompanyVerificationResponseDto[]>(this.restfulService.url, options).subscribe(data => {
this.companyList = data;
this.onSuccesfulReceivedData(data);
},
error => this.restfulService.handleError(error));
}
但是我得到这个错误:
PhantomJS 2.1.1 (Windows 8 0.0.0) ManagementService should be able to accept a company based on email FAILED
Error: Expected no open requests, found 1: GET http://localhost:8080/adminpage in http://localhost:9876/_karma_webpack_/vendor.js (line 32166)
verify@http://localhost:9876/_karma_webpack_/vendor.js:32166:97
http://localhost:9876/_karma_webpack_/main.js:3356:24
invoke@http://localhost:9876/_karma_webpack_/polyfills.js:6843:31
onInvoke@http://localhost:9876/_karma_webpack_/vendor.js:156148:45
invoke@http://localhost:9876/_karma_webpack_/polyfills.js:6842:40
run@http://localhost:9876/_karma_webpack_/polyfills.js:6593:49
runInTestZone@http://localhost:9876/_karma_webpack_/vendor.js:156369:37
http://localhost:9876/_karma_webpack_/vendor.js:156384:33
invoke@http://localhost:9876/_karma_webpack_/polyfills.js:6843:31
run@http://localhost:9876/_karma_webpack_/polyfills.js:6593:49
http://localhost:9876/_karma_webpack_/polyfills.js:7327:37
invokeTask@http://localhost:9876/_karma_webpack_/polyfills.js:6876:36
runTask@http://localhost:9876/_karma_webpack_/polyfills.js:6643:57
drainMicroTaskQueue@http://localhost:9876/_karma_webpack_/polyfills.js:7050:42
我希望它能通过说request方法是一个get请求,但实际结果是一个错误。