假设我有一个使用HttpClient的服务,
@Injectable()
export class MyService {
constructor(protected httpClient: HttpClient) { .. }
}
Run Code Online (Sandbox Code Playgroud)
然后是一个使用此服务的组件.
@Component({
selector: 'my-component'
})
export class SendSmsComponent {
constructor(private MyService) { .. }
}
Run Code Online (Sandbox Code Playgroud)
如何在模拟HttpClient而不是整个服务的同时测试这个组件?
TestBed.configureTestingModule({
declarations: [MyComponent],
providers: [
{ provide: MyService, useClass: MyService } // ?
]
}).compileComponents();
httpMock = TestBed.get(HttpTestingController); // ?
Run Code Online (Sandbox Code Playgroud)