1

This is my spec.ts file. I'm stuck with the error:

Error: Can't resolve all parameters for RequestOptions: (?)

I have imported all the providers necessary also. Can anyone please help me resolve this error?

import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { ResetPasswordComponent } from './reset-password.component';
import { ConfigService } from './../config-service.service';
import {Http, Headers, ConnectionBackend, RequestOptions} from '@angular/http';

describe('ResetPasswordComponent', () => {
  // let component: ResetPasswordComponent;
  // let fixture: ComponentFixture<ResetPasswordComponent>;

   beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [ResetPasswordComponent, ConfigService, Http, ConnectionBackend, RequestOptions]
    });
  });

  // beforeEach(async(() => {
  //   TestBed.configureTestingModule({
  //     declarations: [ ResetPasswordComponent ]
  //   })
  //   .compileComponents();
  // }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ResetPasswordComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  // it('should create', () => {
  //   expect(component).toBeTruthy();
  // });

  it('should create', () => {
    expect('holaa').toBe('holaa');
  });

  it('Is Password Change Function Working', inject([ResetPasswordComponent], (reset:ResetPasswordComponent) => {
    expect(reset.simplyAFunction()).toBe(true);
  }));
});

1 Answer 1

4

Similar situation i got when i used HttpHeaders, Hope my experience will help you. I unit testing always try to mock the services whether it is in build service or written us.

Hence in this case you can mock RequestOptions like this. change your providers as

providers: [ResetPasswordComponent, ConfigService, Http, ConnectionBackend, { provide: RequestOptions, useValue: RequestOptionsMock }]

By doing this in unit testing system will look for mocked RequestOptions rather than original one and hance we can write our own mocked RequestOptions similar providing the properties we have used in our code

A sample mock object could be like this

 const RequestOptionsMock = function(){
     return 'mockedResponse';
}

Additional note This is the original RequestOptions from angular documentation https://angular.io/api/http/RequestOptions

class RequestOptions {
constructor(opts: RequestOptionsArgs = {})
method: RequestMethod | string | null
headers: Headers | null
body: any
url: string | null
params: URLSearchParams
search: URLSearchParams
withCredentials: boolean | null
responseType: ResponseContentType | null
merge(options?: RequestOptionsArgs): RequestOptions
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.