【发布时间】:2019-08-19 13:11:19
【问题描述】:
我有一组类以这种方式接收角度之外的一些依赖项。
import {TypeATest} from '...../TypeA.model'
import { TypeBTest } from '..../TypeB.model'
import { SomeDependency } from './services/SomeDependency'
import { SomeAnother } from './services/SomeAnother'
// ....
@Injectable({
providedIn: 'root'
})
export class TestingService {
this.activeTest: AnyTestType;
constructor(private readonly injectorService: InjectorService) {}
loadTest(TypeOfTest) {
const someDependency = this.injectorService.get(SomeDependency)
const someAnother = this.injectorService.get(SomeAnother)
switch(TypeOfTest) {
case TypeA:
injector
this.activeTest = new TypeATest(someDependency, someAnother);
break;
case TypeB:
this.activeTest = new TypeBTest(someAnother);
break;
}
}
startTest(){
this.activeTest.start()
}
// .. more this.activeTest uses...
}
我正在对加载该外部类的服务进行单元测试,但我不想创建 TypeATest、TypeBTest 或类似的,而只是模拟结果(它们都有相同的 API)但我找不到如何嘲笑他们。有没有办法做到这一点?
【问题讨论】:
-
请使用更多代码更新您的问题。具体来说,上述 switch 语句在您的代码中的什么位置以及如何访问
TypeATest和TypeBTest(它们是使用依赖注入传递还是静态导入?)现在我们看不到足够的代码来回答。 -
嗨@brian-lives-outdoors,我已经更新了问题。