[英]Create list of imports service provider on unit testing
I'm new to Angular, and I have a new team that told me to implement unit testing on an old project with Angular 8. So after investigating that, I decided to use Karma + Jasmine, and I created .spect.ts
document like this:我是 Angular 的新手,我有一个新团队告诉我用 Angular 8 对一个旧项目实施单元测试。所以在调查之后,我决定使用 Karma + Jasmine,我创建了
.spect.ts
文档这个:
describe("CashFlowSalariesComponent", () => { let fixture: ComponentFixture < CashFlowSalariesComponent >; let instance; let profile: ProfileModel; beforeEach(async() => { TestBed.configureTestingModule({ schemas: [CUSTOM_ELEMENTS_SCHEMA], imports: [ RouterTestingModule, FormsModule, ReactiveFormsModule, BrowserModule, HttpClientModule, ToastrModule.forRoot({ positionClass: "toast-bottom-right", }), ], declarations: [CashFlowSalariesComponent], providers: [ ApiService, UserService, ProfileService, VettingStatusService, SkillService, ApplicationRoleService, SeniorityLevelService, PlacementStatusService, EducationLevelService, UtilsService, ShirtSizeService, GenderService, TimeZoneService, CountryService, CityService, PostalCodeService, StateService, ClientSectorService, JobService, ClientService, PulsecheckQuestionService, PulsecheckMasterService, ExchangeRateService, DepartmentService, ExpenseService, ProfileRoleService, SkillCategoriesService, ProfileActivityService, ProfileSalaryActivityService, TimeSheetService, HolidayService, RequestTimeOffService, TimeOffTypeService, InvoiceService, PulsecheckDetailService, ], }).compileComponents(); fixture = TestBed.createComponent(CashFlowSalariesComponent); instance = fixture.componentInstance; fixture.detectChanges(); profile = new ProfileModel(); (instance as any).exchangeRate = 18.92; }); afterEach(() => { fixture.destroy(); }); it("To test instance creation of component", () => { expect(instance).toBeTruthy(); });
As you can see, I had to import many services from providers because my ApiService had injected them, so if I no add them, it returned an error like this:如您所见,我不得不从提供者导入许多服务,因为我的 ApiService 已经注入了它们,所以如果我不添加它们,它会返回如下错误:
NullInjectorError: No provider for {serviceName}!
NullInjectorError:{serviceName} 没有提供者!
So each time I call the ApiService on each component, developers should copy and paste from this component all the services.因此,每次我在每个组件上调用 ApiService 时,开发人员都应该从该组件复制并粘贴所有服务。 So my question is: is this a way to create a class or something to import all providers in once without copying and pasting code each time?
所以我的问题是:这是一种创建 class 的方法,还是可以一次性导入所有提供程序而无需每次都复制和粘贴代码的方法? How can I achieve this?
我怎样才能做到这一点?
There are 2 things that could help you with your issue:有两件事可以帮助您解决问题:
providedIn: 'root'
in this case you don't need to provide them in any module or test.providedIn: 'root'
在这种情况下您不需要在任何模块或测试中提供它们。 Angular would resolve them automatically. example:例子:
@Injectable({ providedIn: 'root' })
export class UserService {}
test.ts
file.test.ts
文件中定义全局提供程序。 example:例子:
@NgModule({
imports: [RouterTestingModule, HttpClientTestingModule],
providers: [...],
})
class GlobalTestingSetupModule {}
TestBed.initTestEnvironment([BrowserDynamicTestingModule, GlobalTestingSetupModule], platformBrowserDynamicTesting());
Also, instead of HttpClientModule
it is preferable to use HttpClientTestingModule
in unit tests.此外,在单元测试中最好使用
HttpClientTestingModule
而不是HttpClientModule
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.