[Angular version: 2.4.5]
As I dive into writing a unit test for an Angular integration component that involves a service using the Angular HTTP library, I'm encountering a challenge with the instantiation of my Service class. Either the Service ends up being undefined or I face the error message "Error: Cannot resolve all parameters for 'RequestOptions'(?)".
I am aware that in such scenarios, it is common practice to mock the backend (which I have successfully done in other unit tests) or utilize Protractor. However, previous answers on Stack Overflow seem to pertain to Angular 1 or solutions that do not quite resolve my issue. Below is the initial code snippet I am working with:
@Injectable()
export class ProjectService {
projectFeature: IProjectFeature;
constructor(private http: Http) { }
getProjects(): Observable<IProjects> {
return this.http.get("/api/Projects")
.map((response: Response) => response.json() || {})
.catch(this.handleError);
}
}
// Jasmine spec file:
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
Http,
ProjectService, // service that uses HTTP
ConnectionBackend
//RequestOptions // commented out to avoid parameter resolution error
],
imports: [
HttpModule
]
});
}));
it('sample test', async(() => {
var projectService = TestBed.get(ProjectService);
var comp = new ProjectComponent(projectService);
comp.ngOnInit(); // this internally triggers projectService.getProjects()
expect(comp.projects[0].name).toEqual("Sample Project Name");
}));