【问题标题】:Angular 2 - Error Invalid provider unit testAngular 2 - 错误无效的提供者单元测试
【发布时间】:2016-12-15 05:29:23
【问题描述】:

我正在开发一个由 Angular-Cli 生成的 Angular 2 RC4 项目,我在每个单元测试中都有相同的错误。

错误:无效的提供者 - 只有提供者和类型的实例是 允许,得到:[object Object](第 170 行)

这是一个规范的简单示例:

import {addProviders,inject} from "@angular/core/testing";
import {AppComponent} from "./app.component";

beforeEach(() => {
  addProviders([AppComponent])
});

describe('Component : App', () => {
  it('should created an instance',
    inject([AppComponent], (app:AppComponent) => {
      expect(app).toBeTruthy();
    }));

});

此错误出现在需要提供程序的每个规范上。

这里是app.component.ts的组件

import { Component } from '@angular/core';
import  { EnvComponent } from "./env/env.component"

// Routage
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives: [ROUTER_DIRECTIVES, EnvComponent],
})


export class AppComponent {

}

PS:测试在 Angular 2 RC3 中正常运行

【问题讨论】:

    标签: javascript angular unit-testing typescript


    【解决方案1】:

    您只需要使用提供者或类型作为addProviders 方法的参数:

    addProviders([AppComponent])
    

    看来AppComponent 不是一个类……app.component 模块的内容是什么?

    【讨论】:

    • 在 RC3 中,测试是使用 beforeEachProviders() 进行的,现在已弃用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2017-02-21
    • 1970-01-01
    • 2015-07-26
    • 2015-04-03
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多