【发布时间】:2018-03-05 19:08:04
【问题描述】:
我在测试使用 karma+jsamine 后出现了这种错误。 服务:ContactService 应该返回可用的语言失败 错误:无法解析 ContactService 的所有参数:(?)。 我不知道是什么导致了这个。
import {Http} from "@angular/http";
const CONTACT_URL = "./pages/contacts.json"
export class ContactService{
constructor(private http:Http) { }
public getContactById(id: number) {
// return this.get(CONTACT_URL, { id: id });
}
get(){
return this.http.get(CONTACT_URL).map(response => response.json())
}
}
import {inject,TestBed,async} from "@angular/core/testing";
import {ContactService} from "./ContactService.component"
import {HttpModule} from "@angular/http";
describe('Service: ContactService', () => {
let service;
beforeEach(() => TestBed.configureTestingModule({
imports:[HttpModule],
providers: [ ContactService ]
}));
beforeEach(inject([ContactService], s => {
service = s;
}));
it('should return available languages',async(() => {
service.get().subscribe(x=> {
expect(x).toContain('en');
expect(x).toContain('es');
expect(x).toContain('fr');
expect(x.length).toEqual(3);
});
}));
});
以下是我的代码。
【问题讨论】:
-
尝试
Injectable或@Inject(Http)并检查您的 tsconfig
标签: angular testing jasmine karma-runner