27

I am trying to return an Observable from a service with mock data.

I am returning this from my service :

return Observable.of(new Object()).map(MOCKACCOUNT =>JSON.stringify(MOCKACCOUNT));

I get an error

Observable_1.Observable.of is not a function.

Am I missing some include? I am importing

import {Observable} from "rxjs/Observable";

Note: I was returning a mock promise prior but based on my understanding I would not be able to interpolate the value. For example {{returnFromServiceStoredInExportedClass.name}}

SnareChops
  • 13,175
  • 9
  • 69
  • 91
Chris
  • 1,299
  • 3
  • 18
  • 34
  • I have a sample where I mock an Observable here: http://www.syntaxsuccess.com/viewarticle/angular-2.0-unit-testing – TGH Feb 17 '16 at 03:42

3 Answers3

25

Looks like

import {Observable} from "rxjs/Observable";

should be

import {Observable} from "rxjs/Rx";

See also Angular2 RxJS getting 'Observable_1.Observable.fromEvent is not a function' error

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thank you for your help. – Chris Feb 17 '16 at 12:53
  • 5
    Interesting. What's the difference between the `Observable` from `rxjs/Observable` and the `Observable` from `rxjs/Rx`? – Jason Swett Mar 05 '17 at 09:35
  • `import { Observable } from 'rxjs/Observable'` will work only as the interface. `import {Observable} from "rxjs/Rx"` is the function itself along with all of the operators. – Nate May Jun 18 '17 at 23:54
5

Use

return Observable.of(new Object()).mapTo(MOCKDATA);`

The import statement is fine.

import {Observable} from "rxjs/Observable";

Also need to import the ts file for MOCKDATA

import {MOCKDATA} from "../path_to_mockdata";
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
1

Should be

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';

because

import {Observable} from "rxjs/Rx";

will import all other operators that you don't need

Alexander
  • 3,019
  • 1
  • 20
  • 24