Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock a service which has http requests? #59

Open
jainAdijain opened this issue Jul 24, 2017 · 2 comments
Open

How to mock a service which has http requests? #59

jainAdijain opened this issue Jul 24, 2017 · 2 comments

Comments

@jainAdijain
Copy link

jainAdijain commented Jul 24, 2017

Hey,
I know this is not the right platform to ask this, but on right platform, there is no reply from anyone.
How to mock a service which contains http requests?
For eg:

I want to test my provider class, so I am unable to write spec for providers.
My provider is as follows:
service.ts

//imports are done correctly.  
@Injectable() 
export class Service {     
constructor(private http: Http) { }      
getGoogle():Observable<any> {        
 console.log("Inside service");       
  return this.http.get('https://jsonplaceholder.typicode.com/posts/1');   
  } }

My page.ts is as follows:

page.ts

**//imports are done correctly.

  export class Page1 {  
  constructor(private service: Service, private navCtrl: NavController) { }   
async get() {     console.log("inside get method");     
const data =  await this.service.getGoogle().toPromise();       
console.log('The response is' , data);       
 } } 

service.spec.ts

//imports are done correctly  

describe('Service', () => 
{     let comp: Service;    
 let fixture: ComponentFixture<Service>;    
 let de: DebugElement;      
 beforeEach(async(() => {         
TestBed.configureTestingModule({             
declarations: [],             
imports: [               
 IonicModule.forRoot(Service)           
   ],            
 providers: [Http]       

  }).compileComponents();   

   }));     

beforeEach(() => {        

 fixture = TestBed.createComponent(Service);      
  comp = fixture.componentInstance;         
 de = fixture.debugElement;   
  });    

 afterEach(() => {       

  fixture.destroy();    

 }); 

it('test the http request to te server', ()=>{           

   //code to test http request of the Service class  }); 

 });** 

All the imports are done correctly, only the logic to test the getGoogle() with http.get() needs to be tested.Please help or share some links or tell me some steps in order to do testing of this ionic2 content.

Thanks
Adi

@wspresto
Copy link

wspresto commented May 6, 2018

Angular is an opinionated framework. Their opinion is that unit testing should never make http requests against a live server.

@jaalzateolaya
Copy link

All you need to do is to mock your HTTP service with a Spy and expect the provider being tested (Service) to call #get with the correct argument (the actual URI) since you are not really testing the connection, you just want to test the communication between your provider and HTTP service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants