Тестирование Angular2 с Jasmine. Как правильно загрузить зависимости?

У меня очень простой тест. Оно работает.

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

person Nick    schedule 31.03.2016    source источник
comment
Не могли бы вы добавить свою конфигурацию для загрузки зависимостей? Вы используете Карму, верно? Кроме того, обязательно импортируйте describe и it из angular2/testing. Angular2 исправляет большинство/все функции jasmine.   -  person Eric Martinez    schedule 01.04.2016


Ответы (1)


В моем случае работает нормально. Посмотри сюда,

рабочая демонстрация

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);
  });
});
person micronyks    schedule 01.04.2016