spyOn(Observable, 'fromEvent')
expect(Observable.fromEvent).toHaveBeenCalled();
Above mentioned piece of code is not working for rxjs6 version but working on rxjs5
import {fromEvent} from 'rxjs'
let Obj = { observableFromEvent: fromEvent }
spyOn(Obj, 'observableFromEvent')
expect(observableFromEvent).toHaveBeenCalled();
/* jasmine test case failed with spy hasn't been called */
```js
import * as rxjs form 'rxjs'
spyOn(rxjs, 'fromEvent')
expect(rxjs.fromEvent).toHaveBeenCalled();
/* Test case failed with no setter or non writable fromEvent */
So you want to spy on fromEvent?
You should be able to just import everything, and spy on that:
import * as rxjs from 'rxjs';
spyOn(rxjs, 'fromEvent')
@benlesh I already did but, Jasmine is complaining [method_name] is not declared writable or has no setter since es6 imports are constants. any other workaround ?
After using @benlesh idea, I am getting below
Error: <spyOn> : fromEvent is not declared writable or has no setter
Usage: spyOn(<object>, <methodName>)
at Object.<anonymous> src/app/app.component.spec.ts:215:7)
at TestBed.push../node_modules/@angular/core/fesm5/testing.js.TestBed.execute node_modules/@angular/core/fesm5/testing.js:1074:1)
at Object.<anonymous> node_modules/@angular/core/fesm5/testing.js:1225:29)
at Object.<anonymous> node_modules/zone.js/dist/fake-async-test.js:593:1)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/proxy.js:128:1)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:387:1)
Getting same error after importing everything.
just use jasmine marbles for that, you can do it like that:
const myEvents = hot('--a-', { a: event });
const expected = cold('--b', { b: result });
expect(component.someSource$).toBeObservable(expected);
@hannaraczek Observable.fromEvent() is from RXJS 5, they switched to just fromEvent() in RXJS 6... As this question is specifically for getting RXJS 6 to work, that is not a viable solution here.
I know this is a lame solution but maybe just wrapping fromEvent would be enough (I didn't test this):
import {fromEvent as realFromEvent} from 'rxjs'
...
it('test', () => {
let called = false;
const fromEvent = (...args: any[]) => {
const o = realFromEvent(...args);
called = true;
return o;
};
...
expect(called).toBeTruthy(); // or whatever...
})
Most helpful comment
@benlesh I already did but, Jasmine is complaining [method_name] is not declared writable or has no setter since es6 imports are constants. any other workaround ?