旧代码使用rxjs v5.5.12,我们将相同的代码复制到了使用rxjs v6.4.0的新项目中。当我们尝试运行测试用例时,我们得到了这个错误。
旧代码:
import * as ObservableEvents from 'rxjs/Observable/fromEvent';
spyOn(ObservableEvents, 'fromEvent').and.returnValue(new Subject<any>().asObservable());
Run Code Online (Sandbox Code Playgroud)
新代码:
import * as rxjs from 'rxjs';
spyOn(rxjs, 'fromEvent').and.returnValue(new Subject<any>().asObservable());
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我们都会收到此错误:
错误:: fromEvent未声明为可写或没有设置器
我们找不到有效的资源来解决此问题。
更新#1
我们尝试使用
import * as rxjs from 'rxjs';
spyOn(jasmine.createSpyObj(rxjs), 'fromEvent').and.returnValue(new Subject<any>().asObservable());
Run Code Online (Sandbox Code Playgroud)
但是这次,我们得到了
createSpyObj需要一个非空数组或方法名称对象来创建用于抛出的间谍
更新#2:
我们使用了@ Omair-Nabiel的代码,现在收到一个新错误
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)
Run Code Online (Sandbox Code Playgroud)
xxx.directive.ts
line 113-> this.componentRef && this.componentRef.destroy();
this.componentRef = null;
line 70-> constructor(
...
private resolver: ComponentFactoryResolver,
...
) …
Run Code Online (Sandbox Code Playgroud)