每当我尝试在使用TestBed.configureTestingModule方法的Angular2应用程序中创建测试模块时,我都会遇到错误。这只有在我尝试将测试中的组件包含在方法中时才会发生。当我尝试加载诸如Http类的提供程序时,也会发生这种情况。我已经在这里待了一天半,到目前为止还没有找到答案。这是我的代码:
我的产品服务。这包含对另一个服务HttpInterceptor的依赖,它本身扩展了Http类。当我删除这些DI时,问题就消失了:
import { Injectable, EventEmitter } from "@angular/core";
import { RequestOptions } from "@angular/http";
import { Product } from "../classes/Product";
import { Observable } from "rxjs";
import { routes } from "../routes";
import { StringHelper } from "../helpers/StringHelper";
import { HttpHelper } from "../helpers/HttpHelper";
import { HttpInterceptor } from "./HttpInterceptor.service";
@Injectable()
export class ProductService {
public emitter: EventEmitter<any> = new EventEmitter();
private APIUrl:String;
constructor(
private _http: HttpInterceptor
) {
}
getProduct(id:number): Observable<Product> {
let headers = HttpHelper.createAuthorizationHeader(true, false);
let options = new RequestOptions({ headers: headers });
return this._http
.get(routes.api.products + '/' + id, options)
.map(res => res.json());
}
/**
* Get all products
* return Observable
*/
getProducts(filters:Object): Observable<Product[]> {
let headers = HttpHelper.createAuthorizationHeader(true, false);
let options = new RequestOptions({ headers: headers });
return this._http
.get(routes.api.products + StringHelper.convertVarsToString(filters), options)
.map(res => res.json());
}
editProduct(payload) {
let headers = HttpHelper.createAuthorizationHeader(true, false);
let options = new RequestOptions({ headers: headers });
payload['_method'] = 'PUT';
return this._http
.post(routes.api.products + '/' + payload.product.id, payload, options)
.map(res => res.json());
}
/**
* Add products to EventEmitter stream
*/
public emitProducts(products): void {
this.emitter.emit(products);
}
}
测试模块。您可以看到我正在尝试使用configureTestingModule来声明正在测试的服务,并且还包括HttpInterceptor类作为提供者,因为这是DI在产品服务服务中的。
import {ProductService} from "../../services/product.service";
import {TestBed} from "@angular/core/testing";
import {HttpInterceptor} from "../../services/HttpInterceptor.service";
import {XHRBackend, RequestOptions} from "@angular/http";
import {AuthService} from "../../services/auth.service";
import {GlobalEventsManager} from "../../services/globalEventsManager.service";
describe('Service: productService', () => {
let prodService: ProductService;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ProductService],
providers: [
{
provide: HttpInterceptor,
useFactory: (
backend: XHRBackend,
defaultOptions: RequestOptions,
authService: AuthService,
globalEventsManager: GlobalEventsManager
) =>
new HttpInterceptor(backend, defaultOptions, authService, globalEventsManager),
deps: [XHRBackend, RequestOptions, AuthService, GlobalEventsManager]
}
]
});
});
});
我在控制台中遇到的错误是:
zone.js:388 Unhandled Promise rejection: Cannot read property 'error' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'error' of undefined TypeError: Cannot read property 'error' of undefined
consoleError @ zone.js:388
zone.js:390 Error: Uncaught (in promise): TypeError: Cannot read property 'error' of undefined
TypeError: Cannot read property 'error' of undefined
从坐在这里挣扎了一天左右我发现,当我尝试测试或包含具有依赖性的组件和服务时会抛出错误。谁能告诉我哪里出错了?
由于