One of the services I have returns all City data from a web service.
@Injectable()
export class CityService {
constructor(private http: Http, private router: Router,
private auth: AuthService) { }
public getAllCity(): Observable<City[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.getAllCity), {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else {
return res.StatusDescription.map(city => {
return new City(city);
});
}
});
}
}
In order to test this service, I attempted the following code. Now, I am looking for advice on how to effectively test the CityService
.
describe('Service: City', () => {
let service: CityService;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [],
providers: [CityService, AuthService, Http, ConnectionBackend],
imports: [RouterTestingModule, HttpClientTestingModule, HttpModule]
});
});
beforeEach(() => {
service = TestBed.get(CityService);
});
it('#getAllCity should return real value', () => {
expect(service.getAllCity()).toBe('real value');
});
});
While running this code, I encountered the following error:
TypeError: Cannot read property 'token' of null
Can you provide any examples or tutorials that can guide me on how to effectively test and display the city data in ng test
?
This is my first attempt at testing, any suggestions or examples would be greatly appreciated.