У меня есть компонент ValidateSessionComponentwhich uses another component 'LoginFormComponent'. 'LoginFormComponent' emits a value which
ValidateSessionComponentshould get should then call
signinmeethod of the
UserManagementService. Я написал следующий тестовый пример, но получаю сообщение об ошибке «Ошибка: StaticInjectorError [LoginFormComponent]: NullInjectorError: Нет поставщика для LoginFormComponent!»
Почему появляется эта ошибка и как ее исправить.
describe('ValidateSessionComponent tests with route parameters', () => {
let component: ValidateSessionComponent;
let fixture: ComponentFixture<ValidateSessionComponent>;
beforeEach(async()=>{
TestBed.configureTestingModule({
declarations: [ LoginFormComponent, //I AM INJECTING THE LOGINFORMCOMPONENT HERE
NavComponentComponent,
ContentComponentComponent,
FooterComponentComponent,
HomepageContentComponentComponent,
ShowErrorsComponent,
ProgressBarComponent,
SignupComponentComponent,
HomepageContentComponentComponent,
NewPracticeQuestionComponent,
PraticeQuestionListComponent,
QuestionDetailsComponent,
PageNotFoundComponent,
DialogBoxComponent,
ValidateSessionComponent],
imports:[ReactiveFormsModule,
RouterModule,
HttpClientModule,
AppRoutingModule
],
providers:[
{provide: UserManagementService, useClass: MockUserManagementService},//mock user management service
HelperService,
DialogBoxService,
{provide: APP_BASE_HREF, useValue: '/'}
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ValidateSessionComponent);
component = fixture.componentInstance;
//using component['activatedRoute'] instead of component.route to remove editor's error that route is private.
/*
this is the way to return different values from a spy based on the received argument. The route will have
params like
{
username:..
redirect-to:...
additional-redirect-info:..
}
*/
spyOn(component['activatedRoute'].snapshot.paramMap,'get').and.callFake(function() {
let param = arguments[0];
console.log('param in fake function ',param);
if(param==='username'){
return '[email protected]'
}
if(param==='redirect-to'){
return '/home'
}
if(param==='additional-redirect-info'){
return {somearg:'somevalue'}
}
console.log('returning unknown param',param);
return param;
});
fixture.detectChanges();
});
fit('should send signin request on receiving form values ',(done)=>{
let loginComponent:LoginFormComponent = TestBed.get(LoginFormComponent);
let userService:UserManagementService = TestBed.get(UserManagementService);
loginComponent.formOutputEvent.emit(new LoginFormValues('[email protected]','somepassword'));
spyOn(userService,'signinUser');
setTimeout(()=>{
expect(userService.signinUser).toHaveBeenCalledWith(new UserSigninInfo('[email protected]','somepassword'));
done();
},1000);
});
});
В spec
, если я удалю LoginFormComponent
(чей селектор app-login-form
), я получаю сообщение об ошибке «Невозможно привязать к 'userId', поскольку это не известное свойство 'app-login-form'.
RouterTestingModule
? - person Amir Christian   schedule 06.10.2019