【问题标题】:Error: Can't resolve all parameters for ContactService: (?)错误:无法解析 ContactService 的所有参数:(?)
【发布时间】: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


【解决方案1】:

改变这个

import {Http} from "@angular/http";
const CONTACT_URL = "./pages/contacts.json"

export class ContactService

import {Http} from "@angular/http";
import {Injectable} from '@angular/core';
const CONTACT_URL = "./pages/contacts.json"

@Injectable()
export class ContactService

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2017-07-30
    相关资源
    最近更新 更多