我目前正在为我的Angular2(带有Typescript)应用程序编写测试,到目前为止一切都很好,直到我试图开始测试我的一个服务。
此服务在实例化时注入了Angular2 Http模块,如下所示:
import { Injectable, EventEmitter } from 'angular2/core';
import { Http } from 'angular2/http';
import 'rxjs/add/operator/map';
import { ConfigObject } from '../ConfigObject';
import { HTTPHelper } from '../helpers/HTTPHelper';
import { Category } from '../classes/Category';
@Injectable()
export class CategoryService {
public emitter: EventEmitter<Category>;
constructor(private _http: Http) {
this.emitter = new EventEmitter();
}
private APIUrl = ConfigObject.productBox + ConfigObject.apiVersion + 'category';
getCategories(filters) {
return this._http.get(this.APIUrl + HTTPHelper.convertVarsToString(filters))
.map(res => res.json());
}
public emitCat(category): void {
this.emitter.emit(category);
}
}
然后用于向我创建的API框发出GET请求。 这是我的服务的Jasmine测试规范文件:
import { CategoryService } from '../../services/category.service';
import { Http } from 'angular2/http';
describe('Category service', () => {
let testCategoryService: CategoryService;
let _http: Http;
beforeEach(function() {
testCategoryService = new CategoryService(Http);
});
it('should have emitter name set', () => {
expect(testCategoryService.emitter._isScalar).toBeDefined();
});
it('should return categories', () => {
testCategoryService.getCategories({
order : 'asc',
order_by : 'name',
parent_id : 0,
});
});
});
正如你所看到的,我也在这里包含了Http对象,并在每行测试之前将它注入我的服务类的测试实例中:
beforeEach(function() {
testCategoryService = new CategoryService(Http);
});
当我尝试在服务类上测试'getCategories'函数时,我收到以下错误:
TypeError: this._http.get is not a function
就我而言,这是奇怪的我将Http服务注入上面一行的测试实例中,所以这应该在类构造函数中设置?
有谁可以看到为什么我的班级中的Http对象没有被设置?
由于