角度2测试中的Observable.from() - 测试方法中的错误

时间:2016-02-07 01:42:56

标签: angularjs angular rxjs5

我必须在我的测试和这个小代码中使用Observable:

it("should emit values", () => {
   const f = Observable.of([1, 3, 5]);
   f.subscribe(d => console.log(d));
});

给出以下错误:

  

TypeError:undefined不是构造函数(评估' Observable_1.Observable.of([1,3,5])')

但是如果我将这两行代码放在一个组件中(例如,在ngOnInit中),我会看到发出的值和没有错误。我想知道这个我或角度2是否在测试代码库方面存在一些问题?

以防万一,这些都是我在这个特定测试文件中使用的导入:

import {
it,
inject,
injectAsync,
beforeEachProviders,
TestComponentBuilder
} from 'angular2/testing';

import {ComponentFixture, dispatchEvent, fakeAsync, tick} from 'angular2/testing_internal';

import {provide, bind} from 'angular2/core';
import {FormBuilder} from 'angular2/common';
import {By} from 'angular2/platform/browser';
import {Observable} from 'rxjs/Observable';
import {ArrayObservable} from 'rxjs/observable/fromArray';
import {Subject} from 'rxjs/Subject';
import {BehaviorSubject} from 'rxjs/subject/BehaviorSubject';

import {SpyLocation} from 'angular2/src/mock/location_mock';
import {RootRouter} from 'angular2/src/router/router';
import {Router, RouterOutlet, RouterLink, RouteParams, ComponentInstruction, RouteData, Location, ROUTER_PRIMARY_COMPONENT} from 'angular2/router';
import {
RouteConfig,
Route,
AuxRoute,
AsyncRoute,
Redirect
} from 'angular2/src/router/route_config_decorator';
import {RouteRegistry} from 'angular2/src/router/route_registry';
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
import {ResolvedInstruction} from 'angular2/src/router/instruction';

1 个答案:

答案 0 :(得分:1)

您是否尝试将import {Observable} from 'rxjs/Observable';更改为import {Observable} from 'rxjs/Rx';?这可能有所帮助,因为以这种方式导入它将获得您可能在组件中使用的所有操作。