在Angular 7的beforeEach Karma/Jasmine测试中将服务注入到自定义类中,可以按照以下步骤进行操作:
import { TestBed } from '@angular/core/testing';
import { YourService } from 'your-service-path';
import { YourCustomClass } from 'your-custom-class-path';
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
YourService
]
});
});
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
YourService
]
});
const yourService = TestBed.get(YourService);
yourCustomClass = new YourCustomClass(yourService);
});
export class YourCustomClass {
constructor(private yourService: YourService) {
// 在这里可以使用yourService进行操作
}
}
通过以上步骤,你可以在Angular 7的beforeEach Karma/Jasmine测试中成功将服务注入到自定义类中。请注意,以上代码示例中的"YourService"和"YourCustomClass"需要替换为实际的服务和自定义类的名称,"your-service-path"和"your-custom-class-path"需要替换为实际的文件路径。
领取专属 10元无门槛券
手把手带您无忧上云