简体   繁体   English

失败:没有Reflector的提供者! 使用业力在angular2中运行测试时出错

[英]Failed: No provider for Reflector! error when running tests in angular2 with karma

While attempting to refactor my tests that utilize the TestComponentBuilder to comply with the breaking change made in angular2.0.0-beta.2 (currently using angular 2.0.0-beta.6), I end up with this when following the instructions specified in the changelog: 在尝试重构利用TestComponentBuilder的测试以符合angular2.0.0-beta.2(当前使用angular 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();
    })
  }));
});

The final test that utilizes the TestComponentBuilder fails with the following error and stack trace: 使用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)

The point of failure seems to be when the TestComponentBuilder makes the call to createAsync(SimpleComponent) judging by the above stack trace. 故障点似乎是当TestComponentBuilder通过上述堆栈跟踪判断调用createAsync(SimpleComponent)时。 What strikes me as odd is that the error is referring to not being able to find the provider for Reflector, which I assume should be covered by the inclusion of angular2-polyfills.js , angular2.js and testing.def.js in my karma.conf.js. 令我感到奇怪的是,该错误是指无法找到Reflector的提供程序,我认为应该将其包含在我的业力中,将angular2-polyfills.js angular2.jsangular2.jstesting.def.js包含在内.conf.js。

I'm thinking this might be a bug in angular2, but I'm not sure. 我认为这可能是angular2中的错误,但我不确定。 What makes me think that this is a bug is that I get the impression that with how the DI in angular 2 is encapsulated nothing that I am doing should be able to influence it at this deep of a level. 我认为这是一个错误,是给我的印象是,用角度2中的DI进行封装时,我所做的任何事情都不能在此深度上影响它。 I'd be ecstatic to find out that it is something that I am doing wrong, but the more I dig into this the less I believe this is the case. 我会欣喜若狂地发现这是我做错的事情,但是我越深入研究这个问题,我就越不相信这种情况。 Any help here would be greatly appreciated. 在这里的任何帮助将不胜感激。

It turns out that I was passing the arguments to setBaseTestProviders in the wrong order ie setBaseTestProviders(TEST_BROWSER_APPLICATION_PROVIDERS, TEST_BROWSER_PLATFORM_PROVIDERS) instead of the correct ordering of setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS); 原来,我以错误的顺序将参数传递给setBaseTestProviders ,即setBaseTestProviders(TEST_BROWSER_APPLICATION_PROVIDERS, TEST_BROWSER_PLATFORM_PROVIDERS)而不是setBaseTestProviders(TEST_BROWSER_APPLICATION_PROVIDERS, TEST_BROWSER_PLATFORM_PROVIDERS)的正确顺序setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS); .

It's still very strange to me that I get this DI exception when the order of the arguments is swapped, seems like something that should be caught by the typescript compiler. 对于我来说,仍然很奇怪,当参数的顺序交换时出现了DI异常,这似乎应该由打字稿编译器捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM