6
我正在使用Http
依赖关系测试服务。每个测试看起来是这样的:Angular2服务测试:使用beforeEach注入依赖关系
import { TestBed, async, inject } from '@angular/core/testing';
import { ValidationService } from './validation.service';
import { HttpModule, Http, Response, ResponseOptions, RequestOptions, Headers, XHRBackend } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
describe('DashboardService',() => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
ValidationService,
{ provide: XHRBackend, useClass: MockBackend }
]
});
});
it('should ...',
inject([ValidationService, XHRBackend],
(service: ValidationService, mockBackEnd: MockBackend) => {
mockBackEnd.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify('content')
})));
});
}));
// assertions ...
});
正如你所看到的,我需要在每次注入后端模拟。
是否有可能在每次测试之前使用beforeEach
注入依赖项?
谢谢你,我会尝试两种方式,看看有什么我最适合。 – trichetriche
有没有一种方法可以在不使用TestBed设置的情况下注入Service? –