Reputation: 6206
I have this code in my test:
import {
beforeEach, beforeEachProviders,
describe, xdescribe,
expect, it, xit,
async, inject
} from '@angular/core/testing';
import { UserService } from './user.service';
import { Http, ConnectionBackend, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { LocalStorage, SessionStorage, WEB_STORAGE_PROVIDERS } from 'h5webstorage';
beforeEachProviders(() => [
UserService, Http, Response, Observable, ConnectionBackend, LocalStorage, Headers, RequestOptions
]);
describe('Service: UserService', () => {
it('testtest', inject([UserService], (service) => {
expect('test').toEqual('test');
}));
});
When I run this I get the following error:
Error: Cannot resolve all parameters for 'Response'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Response' is decorated with Injectable.
Is this because of inproper dependency injection in the test? How can I do proper dependency injection in a Jasmine test?
Upvotes: 0
Views: 361
Reputation: 5730
This should fix it for you:
import { provide } from '@angular/core';
import {
beforeEach,
addProviders,
inject
} from '@angular/core/testing';
import { UserService } from './user.service';
import {BaseRequestOptions, Http, Response, ResponseOptions, RequestMethod} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
describe('Service: UserService', () => {
beforeEach(() => addProviders([
UserService,
BaseRequestOptions,
MockBackend,
provide(Http, {
useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
})
]));
it('testtest', inject([UserService], (service) => {
expect('test').toEqual('test');
}));
});
You need to provide Http with a factory. Otherwise you will not be able to mock the responses.
Upvotes: 2