testing - How to Test Angular2 / TypeScript HTTPService without Mock -


import {injectable} '@angular/core'; import {http} '@angular/http';  @injectable() export class httpservice {   result: any;    constructor(private http:http) {   }     public postrequest(){        return this.http.get('http://httpbin.org/get');       } } 

above code, here test:

i do not want mock anything, test real http connection.

edit - new service.spec file:

import {beforeeachproviders, beforeeach, it, describe, expect, inject} '@angular/core/testing'; import {httpservice} '../../providers/http-service/http-service'; import {translateservice} 'ng2-translate/ng2-translate'; import {goal} '../../providers/goal/goal'; import {navcontroller} 'ionic-angular'; import {http_providers, http} '@angular/http';  describe('http service test', () => {        beforeeachproviders(() => {         return [             http_providers,             httpservice         ];     });      it('should return response when subscribed postrequest',         inject([httpservice], (httpservice: httpservice) => {              httpservice.postrequest().subscribe((res) => {                 expect(res.text()).tobe('hello raja');             });      })); }); 

these errors in karma console:

28 06 2016 14:33:32.067:error [chrome 51.0.2704 (mac os x 10.11.4) | http service test | should return response when subscribed postrequest]: typeerror: cannot read property 'getcookie' of null     @ cookiexsrfstrategy.configurerequest (http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/t/9b9439f5f9c1590d3052594bcae9e877.browserify?26719cf22e6406ebc638b6b187c777666dcc5698:36568:81)     @ xhrbackend.createconnection (http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/t/9b9439f5f9c1590d3052594bcae9e877.browserify?26719cf22e6406ebc638b6b187c777666dcc5698:36583:28)     @ httprequest (http://localhost:9876/absolute/var/folders/vy/18sb1wqs60g734bhr75cw9_r0000gn/t/9b9439f5f9c1590d3052594bcae9e877.browserify?26719cf22e6406ebc638b6b187c777666dcc5698:37476:20) 

you need first configure providers mock http backend:

describe('httpservice tests', () => {   beforeeachproviders(() => {     return [       http_providers,       provide(xhrbackend, { useclass: mockbackend }),       httpservice    ]; }); 

then can use mock backend way:

mockbackend.connections.subscribe(   (connection: mockconnection) => {     if (connection.request.url === 'file1.json') {       // simulate error       var err = new responseerror();       err.status = 404;       connection.mockerror(<error>err);     } else {       // simulate successful response       connection.mockrespond(new response(         new responseoptions({           body: ['i love angular2']         })));     }   });  httpservice.postrequest().subscribe((res:respsone) => {   expect(res.text()).tobe('hello raja'); }); 

edit

if want test real connection, use classical http_providers only:

describe('httpservice tests', () => {   beforeeachproviders(() => {     return [       http_providers,       httpservice    ]; }); 

edit1

since call asynchronous, use async:

it('should return response when subscribed postrequest',     async(inject([httpservice], (httpservice: httpservice) => {          httpservice.postrequest().subscribe((res) => {             expect(res.text()).tobe('hello raja');         });  }))); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -