Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return mocked Data in Observable?

Suppose we have this method in a service that return an Observable:

getSearchResults(request: LocationSearchRequest){
      return this.http.get(this.getApiUrl(request))
            .map(res => <LocationSearchResponse> res.json())    
            .catch(this.handleError);    
}

How can I modify this code to return the mocked data ratehr then making an actual GET requesr?

import { MOCKEDDATA } from './mocked-data';

It's not a duplicate question. This has nothing to do with testing, jasmine and angualr2 testing api.

like image 721
Artur Stary Avatar asked Jun 20 '16 11:06

Artur Stary


1 Answers

Xavi's answer is great and simple, but it didn't work for me - I tweaked it like so:

import { of } from 'rxjs';

And then:

return of(MOCKEDDATA);

Hope this helps someone else too!

like image 162
Joel Balmer Avatar answered Nov 12 '22 21:11

Joel Balmer