angular - ng 测试没有 Http 的提供者!错误

标签 angular karma-jasmine

我尝试对我的 Angular2 应用程序进行测试。我还没有制作 mockingservices,所以我尝试使用我的正常服务。

确切的错误:

Error: No provider for Http!
    at injectionError (http://localhost:9876/base/src/test.ts:1538:86) [angular]
    at noProviderError (http://localhost:9876/base/src/test.ts:1576:12) [angular]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_._throwOrNull (http://localhost:9876/base/src/test.ts:3077:19) [angular]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_._getByKeyDefault (http://localhost:9876/base/src/test.ts:3116:25) [angular]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_._getByKey (http://localhost:9876/base/src/test.ts:3048:25) [angular]
    at ReflectiveInjector_.Array.concat.ReflectiveInjector_.get (http://localhost:9876/base/src/test.ts:2917:21) [angular]
    at DynamicTestModuleInjector.Array.concat.NgModuleInjector.get (http://localhost:9876/base/src/test.ts:3864:52) [angular]

我要测试的组件的@component是:

@Component({
  selector: 'app-game-board',
  templateUrl: './game-board.component.html',
  styleUrls: ['./game-board.component.css'],
  providers: [GameBoardService]
})

我试图在 spec.ts 类(测试)中导入 HTTPModule,但我无法让它工作。这是 spec.ts 的代码

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { GameBoardComponent } from './game-board.component';
import { GameBoardService } from "app/services/gameboardservice";

describe('GameBoardComponent', () => {
  let component: GameBoardComponent;
  let fixture: ComponentFixture<GameBoardComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule], //this is where I try to import the HttpModule
      declarations: [ GameBoardComponent ],
      providers: [GameBoardService]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(GameBoardComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

我也在测试类的提供者中试过这个:

providers: [{ provide: Http, useValue: GameBoardService }]

最佳答案

您仍然需要为您的测试类提供 HTTP,并且您可能希望为此使用 MockBackend。

这是我认为您需要添加的行。

import { MockBackend } from '@angular/http/testing';
import { HttpClient } from '@angular/common/http';

在您的 TestBed.configureTestingModule 提供者中:

providers: [
  {provide: HttpClient, deps: [MockBackend]},
  ...
],

关于angular - ng 测试没有 Http 的提供者!错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44282521/

相关文章:

.net - 为什么我们需要 karma-test-shim.js

Angular17 延迟 View 。如何在模板中转义@符号?

javascript - 类型错误 : Cannot read property 'user_mainValue' of undefined

javascript - NodeJS、Angular Seed、Gulp、Jasmine、Karma 问题

Angular 组件继承

javascript - 为什么我不能在@NgModule 中导入 Angular 2 服务?

javascript - 如何测试指令链接中定义的 $on 事件

node.js - Jasmine 测试错误 - TypeError : Cannot read property 'hide' of undefined

angularjs - Angular Karma - 具有独立范围和 Controller 的指令,调用 element.isolateScope() 返回未定义

Angular 2 : Access property of pipe returning object