失败:没有反射器的提供者!使用karma

时间:2016-02-18 22:31:25

标签: angularjs testing dependency-injection angular

在尝试重构我的测试时,利用TestComponentBuilder来符合angular2.0.0-beta.2(目前使用的是角度2.0.0-beta.6)所做的重大更改,我按照说明操作时最终得到了这一点在更改日志中指定:

import{
  it,
  xit,
  expect,
  describe,
  injectAsync,
  TestComponentBuilder,
  setBaseTestProviders
} from 'angular2/testing';

import {
  TEST_BROWSER_PLATFORM_PROVIDERS,
  TEST_BROWSER_APPLICATION_PROVIDERS
} from 'angular2/platform/testing/browser';

import {Component} from "angular2/core";

@Component({
  selector: 'nope',
  template: `hello`,
})
class SimpleComponent {
  sayHello() {
    return "hello"
  }
}

describe('Simple Component', () => {
  setBaseTestProviders(TEST_BROWSER_APPLICATION_PROVIDERS, TEST_BROWSER_PLATFORM_PROVIDERS);

  it('should recognize a simple component', () => {
    expect(SimpleComponent).not.toBeNull();
    expect(SimpleComponent).not.toBeUndefined();
  });

  it('should get response from sayHello method', () => {
    const simpleComponent = new SimpleComponent();
    expect(simpleComponent.sayHello()).toEqual("hello")
  });

  it('should render simple component', injectAsync([TestComponentBuilder], (tcb:TestComponentBuilder) => {
    return tcb.createAsync(SimpleComponent).then(fix => {
      fix.detectChanges()
      const compiled = fix.debugElement.nativeElement;

      expect(compiled).toBeDefined();
    })
  }));
});

利用TestComponentBuilder的最终测试失败,出现以下错误和堆栈跟踪:

Failed: No provider for Reflector! (DynamicComponentLoader -> Compiler -> RuntimeCompiler -> TemplateCompiler -> TemplateParser -> Parser -> Reflector)
    Error: DI Exception
        at NoProviderError.BaseException [as constructor] (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:7351:21)
        at NoProviderError.AbstractProviderError [as constructor] (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:1649:14)
        at new NoProviderError (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:1673:14)
        at Injector._throwOrNull (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11547:15)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11592:19)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector._getByDependency (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11528:21)
        at Injector._instantiate (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11424:32)
        at Injector._instantiateProvider (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11395:21)
        at Injector._new (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11385:19)
        at InjectorDynamicStrategy.getObjByKeyId (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11263:42)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11586:33)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector._getByDependency (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11528:21)
        at Injector._instantiate (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11423:32)
        at Injector._instantiateProvider (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11395:21)
        at Injector._new (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11385:19)
        at InjectorDynamicStrategy.getObjByKeyId (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11263:42)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11586:33)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector._getByDependency (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11528:21)
        at Injector._instantiate (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11425:32)
        at Injector._instantiateProvider (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11395:21)
        at Injector._new (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11385:19)
        at InjectorDynamicStrategy.getObjByKeyId (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11263:42)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11586:33)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector._getByDependency (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11528:21)
        at Injector._instantiate (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11423:32)
        at Injector._instantiateProvider (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11395:21)
        at Injector._new (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11385:19)
        at InjectorDynamicStrategy.getObjByKeyId (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11263:42)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11586:33)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector._getByDependency (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11528:21)
        at Injector._instantiate (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11423:32)
        at Injector._instantiateProvider (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11395:21)
        at Injector._new (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11385:19)
        at InjectorDynamicStrategy.getObjByKeyId (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11263:42)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11586:33)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector._getByDependency (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11528:21)
        at Injector._instantiate (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11423:32)
        at Injector._instantiateProvider (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11395:21)
        at Injector._new (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11385:19)
        at InjectorDynamicStrategy.getObjByKeyId (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11263:42)
        at Injector._getByKeyDefault (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11586:33)
        at Injector._getByKey (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11540:21)
        at Injector.get (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:11340:19)
        at TestComponentBuilder.createAsync (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/testing.dev.js:848:29)
        at eval (/Users/my-username/projects/myproject/myproject-module/spec/module-form.spec.js:54:32)
        at FunctionWrapper.apply (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2.js:327:17)
        at FunctionWithParamTokens.execute (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/testing.dev.js:1919:37)
        at TestInjector.execute (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/testing.dev.js:1864:17)
        at Object.<anonymous> (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/testing.dev.js:1978:44)
        at Object.eval (/Users/my-username/projects/myproject/myproject-module/spec/example.spec.js:5:9)
        at /Users/my-username/projects/myproject/myproject-module/karma.shim.js:55:32
        at Zone.run (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:1243:24)
        at zoneBoundFn (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:1220:26)
        at lib$es6$promise$$internal$$tryCatch (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:468:17)
        at lib$es6$promise$$internal$$invokeCallback (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:480:18)
        at lib$es6$promise$$internal$$publish (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:451:12)
        at /Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:123:10
        at Zone.run (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:1243:24)
        at zoneBoundFn (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:1220:26)
        at lib$es6$promise$asap$$flush (/Users/my-username/projects/myproject/myproject-module/node_modules/angular2/bundles/angular2-polyfills.js:262:10)

失败点似乎是TestComponentBuilder通过上面的堆栈跟踪来调用createAsync(SimpleComponent)。令我感到奇怪的是,错误是指无法找到Reflector的提供者,我认为应该包括angular2-polyfills.jsangular2.jstesting.def.js。我的karma.conf.js。

我认为这可能是angular2中的一个错误,但我不确定。是什么让我觉得这是一个错误,我得到的印象是,角度2中的DI是如何封装的,我正在做的事情应该能够在这个深层次上影响它。我很欣慰地发现这是我做错的事情,但是我越深入了解我就越不相信这种情况。这里的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

事实证明,我正在以错误的顺序将参数传递给setBaseTestProviders,即setBaseTestProviders(TEST_BROWSER_APPLICATION_PROVIDERS, TEST_BROWSER_PLATFORM_PROVIDERS)而不是setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);的正确排序。

我仍然很奇怪,当参数的顺序被交换时,我得到这个DI异常,似乎应该被typescript编译器捕获。