The old code uses rxjs v5.5.12, We copied the same code to our new project which uses rxjs v6.4.0. We are getting this error when we tried to run the test case.
Old Code:
import * as ObservableEvents from 'rxjs/Observable/fromEvent';
spyOn(ObservableEvents, 'fromEvent').and.returnValue(new Subject<any>().asObservable());
New Code:
import * as rxjs from 'rxjs';
spyOn(rxjs, 'fromEvent').and.returnValue(new Subject<any>().asObservable());
In both cases we are getting this error:
Error: : fromEvent is not declared writable or has no setter
We couldn't find a valid resource to solve this issue.
Update #1
We tried using
import * as rxjs from 'rxjs';
spyOn(jasmine.createSpyObj(rxjs), 'fromEvent').and.returnValue(new Subject<any>().asObservable());
but this time, we got
createSpyObj requires a non-empty array or object of method names to create spies for thrown
Update #2:
We used the code from @Omair-Nabiel, now getting a new error
TypeError: Object(...) is not a function
at XxxPopoverDirective.fromEvent [as createPopover] (http://xxx:xxxx/src/app/shared/xxx/xxx.directive.ts?:113:37)
at XxxPopoverDirective.createPopover [as mouseClick] (http://xxx:xxxx/src/app/shared/xxx/xxx.directive.ts?:70:14)
at runTest (http://xxx:xxxx/src/app/shared/xxx/xxx.directive.spec.ts?:181:19)
xxx.directive.ts
line 113-> this.componentRef && this.componentRef.destroy();
this.componentRef = null;
line 70-> constructor(
...
private resolver: ComponentFactoryResolver,
...
) { }
Update #3
Hi Omair Nabiel, Please find the below code we are using, please let me know the solution,
file="popover.directive.ts" Code:
import { fromEvent } from 'rxjs/Observable/fromEvent';
this.clickOutSub = fromEvent(this.documentRef.getDocument(), 'click').subscribe(this.clickOut.bind(this));
file="popover.directive.spec.ts"
Code:
import * as ObservableEvents from 'rxjs/Observable/fromEvent';
function runTest() {
spyOn(ObservableEvents, 'fromEvent').and.returnValue(new Subject<any>().asObservable());
}
it('...', () => {
expect(ObservableEvents.fromEvent).toHaveBeenCalled();
});