简体   繁体   English

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

[英]Error: No provider for HttpService! In Karma test

I have a angular 2 tool to monitor servers and just started with the tests. 我有一个角度2工具来监控服务器,刚开始测试。 When I try to mock the httpService I didnt know how to mock the Rest-API so I looked online, fixed some errors and am now stuck on this one. 当我尝试模拟httpService时,我不知道如何模拟Rest-API,所以我在线查看,修复了一些错误,现在我已经陷入了这个错误。
Here the Error: 这里错误:

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)

Here is my Testcase: 这是我的测试用例:

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();
            })));
});

Thanks for the help :) 谢谢您的帮助 :)

Your syntax seams to be wrong, check docs . 您的语法接缝错误,请检查文档 Something like this should work: 这样的事情应该有效:

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

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

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