使用Jasmine进行Angular2测试。如何加载依赖关系更正?

时间:2016-03-31 21:41:16

标签: jasmine angular angular2-testing

我的测试非常简单。它有效。

describe('Parser', () => {
  it('getData works properly', () => {
    expect(1).toEqual(1);
  });
});

我正在尝试加载相应的依赖项。但它不起作用:

import {beforeEachProviders} from 'angular2/testing';

describe('Parser', () => {
  beforeEachProviders(() => []);  // Don't works with this line
  it('getData works properly', () => {
    expect(1).toEqual(1);
  });
});

为什么?

来自浏览器控制台的错误消息:

Error: @http://0.0.0.0:8000/build/vendors/angular2/src/facade/async.js:10:17
    @http://0.0.0.0:8000/build/vendors/angular2/src/facade/async.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/abstract_change_detector.js:9:15
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/abstract_change_detector.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/dynamic_change_detector.js:9:34
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/dynamic_change_detector.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/proto_change_detector.js:6:33
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/proto_change_detector.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/change_detection.js:36:31
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/change_detection.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection.js:6:26
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/metadata/directives.js:17:26
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/metadata/directives.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/metadata.js:13:20
    @http://0.0.0.0:8000/build/vendors/angular2/src/core/metadata.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/core.js:9:10
    @http://0.0.0.0:8000/build/vendors/angular2/core.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/testing/test_injector.js:1:103
    @http://0.0.0.0:8000/build/vendors/angular2/src/testing/test_injector.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/src/testing/testing.js:6:23
    @http://0.0.0.0:8000/build/vendors/angular2/src/testing/testing.js:1:1
    @http://0.0.0.0:8000/build/vendors/angular2/testing.js:12:10
    @http://0.0.0.0:8000/build/vendors/angular2/testing.js:1:1
    @http://0.0.0.0:8000/build/app/module/parser/parser.spec.js:3:17
    @http://0.0.0.0:8000/build/app/module/parser/parser.spec.js:1:1
    $@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:8738
    H@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:8372
    [7]</</</</R.prototype.when@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:12046
    [7]</</</</b.prototype.run@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:11080
    [3]</</</t.prototype._drain@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:2985
    t/this.drain@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:2652
    e@http://0.0.0.0:8000/build/vendors/systemjs/dist/system-polyfills.js:4:4575
    Zone</ZoneDelegate</ZoneDelegate.prototype.invoke@http://0.0.0.0:8000/build/vendors/angular2/bundles/angular2-polyfills.js:332:20
    Zone</Zone</Zone.prototype.runGuarded@http://0.0.0.0:8000/build/vendors/angular2/bundles/angular2-polyfills.js:241:29
    Zone</Zone</Zone.prototype.wrap/<@http://0.0.0.0:8000/build/vendors/angular2/bundles/angular2-polyfills.js:217:25

    Evaluating http://0.0.0.0:8000/rxjs/Subject.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/facade/async.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/abstract_change_detector.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/dynamic_change_detector.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/proto_change_detector.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection/change_detection.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/change_detection.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/metadata/directives.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/core/metadata.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/core.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/testing/test_injector.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/src/testing/testing.js
    Evaluating http://0.0.0.0:8000/build/vendors/angular2/testing.js
    Evaluating http://0.0.0.0:8000/build/app/module/parser/parser.spec.js
    Error loading http://0.0.0.0:8000/build/app/module/parser/parser.spec.js system-polyfills.js:4:8738

1 个答案:

答案 0 :(得分:1)

在我的情况下工作正常。看看这里,

<强> working demo

import {
  beforeEach,
  describe,
  expect,
  it,
  injectAsync,
  TestComponentBuilder,
  beforeEachProviders,

} from 'angular2/testing';

describe('Parser', () => {
  it('getData works properly', () => {
    expect(1).toEqual(1);  
  });
});

describe('Parser', () => {
  beforeEachProviders(() => []);  // works fine in my case.
  it('getData works properly', () => {
    expect(1).toEqual(1);
  });
});