samedi 19 août 2017

Karma test runs into an error: Can't resolve all parameters for RequestOptions: (?)

I try to run the following Karma Test:

 beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ DetailComponent ],
      imports:[MdToolbarModule,MdSlideToggleModule,MdInputModule, RouterTestingModule, FormsModule , HttpModule],
      providers: [HeatingControlService, Http, ConnectionBackend, RequestOptions]
    })
    .compileComponents();
  }));

I get the following error message from Karma: enter image description here

Descripton of the tested cass

DetailComponent

export class DetailComponent implements OnInit {
  ... some param...

  constructor(private router:Router,private heatingService:HeatingControlService, private location: Location) { }
  .... some methods

Methods in DetailComponent runs methods from HeatingControlService

*HeatingControlService *

@Injectable()
export class HeatingControlService extends BaserequestService {

  constructor(protected http:Http) {
    super(http)
  }

 ... some methods

The BaserequestService contails the standard HTTP request. The reason why I use this as "main-parent" is, the fact of using JWT to authenticate to the backend. In theBaserequestService I set the header option and so I can run all request with the token.

export class BaserequestService  {

  protected headerData: Headers = new Headers();
  protected options: RequestOptions;
  protected bodyData: {}

  constructor(protected http: Http) {  }
  ... some Methods

   protected setHeaderOptions() {
      this.headerData = new Headers()
      this.headerData.append("Content-Type", "application/json");
      this.headerData.append("Accept", "application/json");

      if (localStorage.getItem('user')) {
        let user= JSON.parse(localStorage.getItem('user'));
        this.headerData.append('Authorization', 'JWT ' + user.token);
      }

      this.options = new RequestOptions();
      this.options.headers = this.headerData;
    }

The main question is:

  • How can I fix this error?

But also:

  • Why it runs into the error?
  • Is it a good concept to use inheritance or is better to inject the method?

Aucun commentaire:

Enregistrer un commentaire