相关疑难解决方法(0)

如何在Angular的组件测试中模拟提供的服务中的HttpClient?

假设我有一个使用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)

unit-testing angular

17
推荐指数
2
解决办法
1万
查看次数

标签 统计

angular ×1

unit-testing ×1