2016-10-17 85 views
5

我有一個角度2的工具來監視服務器,並剛剛開始測試。當我嘗試模擬httpService時,我不知道如何模擬Rest-API,所以我看上去在線,修正了一些錯誤,現在我停留在這個上面。
在這裏的錯誤:錯誤:沒有爲HttpService提供程序!在Karma測試

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

感謝您的幫助:)

回答

7

你的語法接縫是錯誤的,檢查docs。像這樣的東西應該工作:

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

首先感謝快速的污水,但錯誤仍然是一樣的。任何其他想法? – Bono

+0

@Bono您仍然需要將'HttpServiceFront'添加到提供者列表 –

相關問題