Поскольку я делаю юнит-тестирование, я должен был высмеять UserManagementService
.Сделать это.Я создал фиктивный класс, который содержит методы и свойства UserManagementService
, которые nav
использует.
class MockUserManagementService {
signInStateSubject:BehaviorSubject<Result> ;
userSignInState$:Observable<Result>;//naming convention for Streams has $ in the end.
constructor(){
this.signInStateSubject = new BehaviorSubject<Result>(new Result('initial',""));//create Observable. Other components can subscribe to it now to get notifications/values
this.userSignInState$ = this.signInStateSubject.asObservable();
}
getUserProfile(userId: UserID){
console.log('mocked getUserProfile called');
}
}
, затем я provide
имитируемый класс вместо реального UserManagementClass
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ...],
imports:[...],
providers:[...,
{provide: UserManagementService,useClass:MockUserManagementService},//mock user management service
})
.compileComponents();
}));
Спецификации такие же, как были раньше.Но на этот раз мне не нужно было делать какие-либо свойства реальной UserManagementService
общедоступной, так как сервис поиздевался!
it('should subscribe to user sign in state observable',()=>{
let navComponent:NavComponentComponent = component;
let userManagementService = TestBed.get(UserManagementService);//component.userManagementService;
console.log('mock service is ',userManagementService);
console.log('subject is ',userManagementService.signInStateSubject);
console.log('observable is ',userManagementService.userSignInState$);
console.log('nav is ',navComponent);
expect(navComponent.userSignInStateSubscription.closed).toBe(false);
});
it('should should set user as signedIn for successful signin attempt',()=>{
let navComponent:NavComponentComponent = component;
/*The idea of unit test is that UserManagementService might not be available yet
so mock UserManagementService
*/
let userManagementService = TestBed.get(UserManagementService);//component.userManagementService;
console.log('got mocked user management service',userManagementService);
//component.subscribeToUserSignInState();
console.log('component is ',navComponent);
navComponent.userLoggedIn = false;
let dummyUserProfile = new UserProfileAPI(new User('manu','chadha','test@test.com'));
console.log('dummy profile is ',dummyUserProfile);
console.log('dummy profile is JSON stringify',JSON.stringify(dummyUserProfile));
let userSignInState = new UserSigninState(true,JSON.stringify(dummyUserProfile));
console.log('user signin state ',userSignInState);
userManagementService.signInStateSubject.next(new Result('success',userSignInState));
//userManagementService.signInStateSubject.next(new Result('success1',(new UserSigninState(true,JSON.stringify(dummyUserProfile))).toString));
expect(navComponent.userLoggedIn).toBe(true)
});
it('should get user\'s profile',()=>{
let userManagementService = TestBed.get(UserManagementService);
let navComponent:NavComponentComponent = component;
console.log('component is ',navComponent);
navComponent.profile = new UserProfileAPI(new User("fn","ln","test@test.com"));
spyOn(userManagementService,'getUserProfile');
component.onProfileClick();
let userId = new UserID(navComponent.profile['external-profile'].email);
expect(userManagementService.getUserProfile).toHaveBeenCalledWith(userId);
});