Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI Jasmine Unit test Error: Can't resolve all parameters for RequestOptions: (?)

First i got this error No provider for ConnectionBackend! so i import it to my spec Although i am not importing it in my service

then i got No provider for RequestOptions! i imported it to my spec also, Although i am not importing it in my service

then i got this error : Can't resolve all parameters for RequestOptions: (?).

any suggestions?

BookingDataService spec

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 spec

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()
    );
  }

}
like image 376
Momen Avatar asked Jan 01 '26 10:01

Momen


1 Answers

In your BookingDataService spec file,

Modify the TestBed.configuration

  • Remove the providerrs Http, RequestOptions
  • import the HttpModule
like image 101
Aravind Avatar answered Jan 05 '26 22:01

Aravind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!