加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

错误:没有HttpService的提供者!在Karma测试中

发布时间:2020-12-17 17:46:33 所属栏目:安全 来源:网络整理
导读:我有一个角度2工具来监控服务器,刚开始测试.当我尝试模拟httpService时,我不知道如何模拟Rest-API,所以我在线查看,修复了一些错误,现在我已经陷入了这个错误. 这里错误: Chrome 53.0.2785 (Windows 10 0.0.0) HttpServiceFront should use an HTTP call Ser
我有一个角度2工具来监控服务器,刚开始测试.当我尝试模拟httpService时,我不知道如何模拟Rest-API,所以我在线查看,修复了一些错误,现在我已经陷入了这个错误.
这里错误:

Chrome 53.0.2785 (Windows 10 0.0.0) HttpServiceFront should use an HTTP call Servers FAILED
        Error: No provider for HttpServiceFront!
            at NoProviderError.Error (native)
            ...
            at drainMicroTaskQueue (webpack:///~/zone.js/dist/zone.js:368:0 <- config/karma-test-shim.js:6854:36)
Chrome 53.0.2785 (Windows 10 0.0.0): Executed 2 of 3 (1 FAILED) (skipped 1) (0.268 secs / 0.057 secs)

这是我的测试用例:

import {
    ResponSEOptions,Response,Http,BaseRequestOptions,RequestMethod
} from '@angular/http';

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

import { HttpServiceFront } from '../app/services/httpServiceFront';

import { MockBackend,MockConnection } from '@angular/http/testing';

const mockHttpProvider = {
    deps: [ MockBackend,BaseRequestOptions ],useFactory: (backend: MockBackend,defaultOptions: BaseRequestOptions) => {
        return new Http(backend,defaultOptions);
    }
};

describe('HttpServiceFront',() => {
    beforeEach(() => {
        {Http,mockHttpProvider}
        TestBed.configureTestingModule(
            [MockBackend,BaseRequestOptions]
        )
    });

    it('should use an HTTP call Servers',inject(
            [HttpServiceFront,MockBackend],fakeAsync((service: HttpServiceFront,backend: MockBackend) => {
                backend.connections.subscribe((connection: MockConnection) => {

                    expect(connection.request.method).toBe(RequestMethod.Get);
                    expect(connection.request.url).toBe(
                        'http://localhost:8080/server');
                });

                service.getServers();
            })));
});

谢谢您的帮助 :)

解决方法

您的语法接缝错误,请检查 docs.这样的事情应该有效:

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [
          { provide: Http,useValue: mockHttpProvider },MockBackend,BaseRequestOptions]
    })
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读