typescript - Jak mockovat Angular subscription v testu Jasmine
Snažím se opravit jednotkový test (nejsem příliš obeznámený s Jasmine), po přidání několika odběrů do služby. Objevuje se mi chyba, že jsou nedefinované. Nejsem si jistý, jak správně zahrnout tyto odběry.
Moje komponenta (je toho víc, ale pro přehlednost jsem to zkrátil):
export class AppComponent implements OnInit, AfterViewInit {
isSignedIn = false;
isLoggedIn = false;
signedInSubscription: Subscription;
loggedInSubscription: Subscription;
constructor(
private authenticationService: AuthenticationService,
private router: Router,
private http403Tester: Http403TestService,
) {
this.isLoggedIn = authenticationService.isLoggedIn;
this.isSignedIn = authenticationService.isSignedIn;
this.setEnvironment();
}
async ngOnInit() {
// line 69 below, this is where the error is
this.signedInSubscription = this.authenticationService.observableIsSignedIn.subscribe(item => {
this.isSignedIn = item;
});
this.loggedInSubscription = this.authenticationService.observableIsLoggedIn.subscribe(item => {
this.isLoggedIn = item;
});
}
}
Služba:
export class AuthenticationService {
constructor() {
this.observableIsSignedIn = new BehaviorSubject<boolean>(this.isSignedIn);
this.observableIsLoggedIn = new BehaviorSubject<boolean>(this.isLoggedIn);
}
isSignedIn: boolean = null;
isLoggedIn: boolean = null;
observableIsSignedIn: any;
observableIsLoggedIn: any;
Specifikace:
describe('AppComponent', () => {
let authenticationService: any;
let http403Tester: any;
let router: any;
let component: AppComponent;
beforeEach(() => {
authenticationService = {
isSignedIn: jasmine.createSpy(),
isLoggedIn: jasmine.createSpy(),
observableIsSignedIn: jasmine.createSpy(),
observableIsLoggedIn: jasmine.createSpy(),
};
router = { navigateByUrl: jasmine.createSpy() };
http403Tester = { isAuthenticated: jasmine.createSpy() };
component = new AppComponent(authenticationService, router, http403Tester);
});
describe('ngOnInit()', () => {
it('should navigate to /unauthorised when a 403 status code is returned', async () => {
// Arrange
http403Tester.isAuthenticated.and.returnValue(false);
// Act
await component.ngOnInit();
// Assert
expect(router.navigateByUrl).toHaveBeenCalledWith('/unauthorised');
});
A chyba:
TypeError: Cannot read property 'subscribe' of undefined
at AppComponent.<anonymous> (src/app/app.component.ts:69:83)
Takže si docela jistě myslím, že createSpy() není správný způsob, jak mockovat odběr:
observableIsSignedIn: jasmine.createSpy()
Ale nemám ponětí, jak pokračovat nebo co hledat. Máš nějaké nápady?
Odpověď č. 1
Zkus to
authenticationService = {
isSignedIn: jasmine.createSpy(),
isLoggedIn: jasmine.createSpy(),
observableIsSignedIn: new BehaviorSubject<boolean>(undefined).asObservable(),
observableIsLoggedIn: new BehaviorSubject<boolean>(undefined).asObservable(),
};
Pro více informací: Mockování BehaviorSubjectu v testovací specifikaci komponenty
Aktualizace :
Ve skutečnosti jste vynechal správnou konfiguraci pro jednotkové testování. Prosím zkontrolujte: https://angular.io/guide/testing#component-dom-testing
