如果使用模拟测试,如何在提供程序中添加多个服务? :Angular 2

时间:2017-07-21 18:56:27

标签: angular

我在Angular 2中遇到了一些关于测试的问题。这是我的代码片段:

app.component.spec.ts

import { KnowledgeServiceApi } from "../knowledgeapi.service";
import { SpeechSynthesisService } from "../speech-synthesis.service";

providers: [
  {provide: KnowledgeServiceApi, useValue: MockKnowledgeApi }
]

我收到的错误:No provider for SpeechSynthesisService

当我尝试这样做时:

providers: [
  {provide: KnowledgeServiceApi, SpeechSynthesisService, useValue: MockKnowledgeApi }
]

我仍然得到错误。我该如何解决这个错误?

1 个答案:

答案 0 :(得分:2)

如果您希望使用相同的模拟,您的provider数组应该如下所示:

providers: [
  { provide: KnowledgeServiceAp, useValue: MockKnowledgeApi },
  { provide: SpeechSynthesisService, useValue: MockKnowledgeApi }
]

但我建议使用这样的另一个:

providers: [
  { provide: KnowledgeServiceAp, useValue: MockKnowledgeApi },
  { provide: SpeechSynthesisService, useValue: MockSpeechSynthesisService }
]