错误:预期没有打开的请求,找到1(角度)

时间:2018-11-28 12:10:13

标签: javascript angular jasmine

我正在尝试为 angular6 中的服务创建测试用例。该服务具有许多不同的ROMAN_LOWER请求方法(http等),并在其中进行了API调用,以获取适当的响应。我正在尝试创建发出模拟get, put, post请求并返回响应的测试用例。但是,我遵循了Tutorial,显然可以帮助我准确地完成自己想做的事情。

但是,当我为该服务运行测试用例时,它给了我以下错误(出于隐私目的,我检查了http中的URL

GET

我尝试浏览This解决方案和This解决方案,但无济于事。

这是我的服务代码:

Error: Expected no open requests, found 1: GET https://staging.xxxxxxxxxx.co.uk/rest/v11_1/oauth2/token
    at HttpClientTestingBackend.push../node_modules/@angular/common/fesm5/http/testing.js.HttpClientTestingBackend.verify (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http/testing.js:326:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/Services/adapter.service.spec.ts:22:13)
    at TestBed.push../node_modules/@angular/core/fesm5/testing.js.TestBed.execute (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:1073:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:1224:29)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:388:1)
    at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:288:1)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:387:1)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:138:1)
    at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:509:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:524:1)

测试用例

import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import {
  HttpHeaders,
    HttpClient,
    HttpParams,
} from '@angular/common/http';
import { Request, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { JwtService } from './jwt.service';

const API_URL = environment.api.host;

@Injectable({
    providedIn: 'root'
})
export class AdapterService {

    constructor(private http: HttpClient, private jwtService: JwtService) {}

    private formatErrors(self: AdapterService) {
        return (res: Response) => {
            return Observable.throw(res);
        };
    }

    private requestHeaders(path: string) {
    let headers;
    if (path !== 'oauth2/token') {
        headers = new HttpHeaders({
          'Accept':  'application/json',
          'Oauth-Token': this.jwtService.getToken()
        })
      }
        return headers;
    }

    get(path: string, params: HttpParams = new HttpParams()): Observable < any > {
    let headers = this.requestHeaders(path);
        return this.http.get(`${API_URL}${path}`, { headers })
            .catch(catchError(this.formatErrors(this)));
    }

    put(path: string, body: Object = {}): Observable < any > {
        return this.http.put(
            `${API_URL}${path}`,
            JSON.stringify(body),
        ).catch(catchError(this.formatErrors(this)));
    }

    post(path: string, body: Object = {}): Observable < any > {
    return this.http.post(
            `${API_URL}${path}`,
            JSON.stringify(body),
        ).catch(catchError(this.formatErrors(this)));
    }

    delete(path): Observable < any > {
    return this.http.delete(
            `${API_URL}${path}`,
        ).catch(catchError(this.formatErrors(this)));
    }
}

1 个答案:

答案 0 :(得分:1)

已解决,我忘了在测试用例中添加对API调用的ExpectOne请求:

backend.expectOne( API_URL + 'oauth2/token').flush(null, { status: 200, statusText:'Ok' });

非常幼稚的观察,对于给您带来的不便表示歉意。