Angular CLI Jasmine Unit测试错误:无法解析RequestOptions的所有参数:(?)

时间:2017-06-18 14:24:38

标签: angular jasmine

首先我收到此错误没有ConnectionBackend的提供程序!所以我将它导入我的规格虽然我没有在我的服务中导入它

然后我没有RequestOptions的提供者!我也将它导入我的规格,虽然我没有在我的服务中导入它

然后我收到此错误:无法解析RequestOptions的所有参数:(?)。

有什么建议吗?

BookingDataService规范

import { TestBed, inject } from '@angular/core/testing';
import { BookingDataService } from './booking-data.service';
import { Http, ConnectionBackend, RequestOptions } from '@angular/http';

describe('BookingDataService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [BookingDataService, Http, ConnectionBackend, RequestOptions]
    });
  });

  it('should be created', inject([BookingDataService], (service: BookingDataService) => {
    expect(service).toBeTruthy();
  }));
});

appComponent规范

import { TestBed, async } from '@angular/core/testing';

import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { FindTripComponent } from './find-trip/find-trip.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        ReactiveFormsModule,
        HttpModule
      ],
      declarations: [
        AppComponent,
        FindTripComponent
      ]
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

});

BookingDataService ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map'

@Injectable()
export class BookingDataService {

  //create instance of http
  constructor(private _http: Http) { }

  //get data from json file
  fetchData() {
    return this._http.get('/assets/mockdata/mock.json').map(
      response => response.json()
    );
  }

}

1 个答案:

答案 0 :(得分:11)

BookingDataService spec文件中,

修改TestBed.configuration

  • 删除提供者Http,RequestOptions
  • 导入HttpModule