【问题标题】:Karma : Angular 4 No provider for Service业力:Angular 4 没有服务提供者
【发布时间】:2018-07-05 01:17:31
【问题描述】:

您好想在我的 Angular 组件中测试真正的服务。但我得到以下错误。 Service blservice 没有提供者

import {async, getTestBed,TestBed, inject } from '@angular/core/testing';

import { BlService } from './bl.service';
import { Injectable } from '@angular/core';
import { BaseRequestOptions, Http, Response, ResponseOptions, XHRBackend} from '@angular/http';

describe('BlService', () => {

  beforeEach(() => {TestBed.configureTestingModule({
    providers: [BlService]
   });
  });

  it('should be created', inject([BlService], (service: BlService) => {
    expect(service).toBeTruthy();
  }));
});

错误:

错误:没有 BlService 的提供者!错误:没有 BlService 的提供者!
在 ReflectiveInjector_.prototype._throwOrNull (http://localhost:9876/_karma_webpack_/vendor.bundle.js:42115:13)
在 ReflectiveInjector_.prototype._getByKeyDefault (http://localhost:9876/_karma_webpack_/vendor.bundle.js:42154:13)
在 ReflectiveInjector_.prototype._getByKey (http://localhost:9876/_karma_webpack_/vendor.bundle.js:42086:13)
在 ReflectiveInjector_.prototype.get (http://localhost:9876/_karma_webpack_/vendor.bundle.js:41955:9) 在 解决NgModuleDep (http://localhost:9876/_karma_webpack_/vendor.bundle.js:48958:5) 在 NgModuleRef_.prototype.get (http://localhost:9876/_karma_webpack_/vendor.bundle.js:50028:9)

【问题讨论】:

  • 谁能帮忙解决这个问题

标签: angular karma-runner


【解决方案1】:

这样解决:

import {async} from '@angular/core/testing';


beforeEach(async(() => {
  TestBed.configureTestingModule({
    providers: [BlService]
  });
}));

【讨论】:

  • 非常感谢。我不太聪明地实现了这个答案。对于像我这样有问题的其他人的快捷方式,请在遇到问题的 describe/it 方法中使用 async(()=>{}) 。不只是在beforeEach上……有时我不太聪明。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 2020-06-23
  • 2020-11-14
  • 2019-11-23
  • 2017-08-22
  • 2023-04-09
  • 2012-07-07
相关资源
最近更新 更多