You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
5 Hrs
17 Min
29 Seg

Pruebas a Guardianes

11/20
Resources

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Los guardianes son los que protegen el ingreso a cierta ruta cuando se aplica una condici贸n, para hacer pruebas de los guardianes se debe hacer lo siguiente.

se puede ver como funciona el guardian con los diferentes servicios

auth.guard.ts

import { Injectable } from '@angular/core';
import { Router, ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { TokenService } from './../services/token.service';
import { AuthService } from './../services/auth.service';

@Injectable({
聽 providedIn: 'root'
})

export class AuthGuard implements CanActivate {
聽 constructor(
聽 聽 private tokenService: TokenService,
聽 聽 private authService: AuthService,
聽 聽 private router: Router
聽 ) {}

聽 canActivate(
聽 聽 route: ActivatedRouteSnapshot,
聽 聽 state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
聽 聽 // const token = this.tokenService.getToken();
聽 聽 // if (!token) {
聽 聽 // 聽 this.router.navigate(['/home']);
聽 聽 // 聽 return false;
聽 聽 // }
聽 聽 // return true;
聽 聽 return this.authService.user$
聽 聽 .pipe(
聽 聽 聽 map(user => {
聽 聽 聽 聽 if(!user) {
聽 聽 聽 聽 聽 this.router.navigate(['/home']);
聽 聽 聽 聽 聽 return false;
聽 聽 聽 聽 }
聽 聽 聽 聽 return true;
聽 聽 聽 })
聽 聽 )
聽 }
}

las pruebas de creaci贸n llevan mockin de los servicios utilizados dentro de auth guard

auth.guard.spec.ts

import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { TokenService } from '../services/token.service';
import { AuthGuard } from './auth.guard';

describe('Test for AuthGuard', () => {
聽 let guard: AuthGuard;
聽 let tokenService: jasmine.SpyObj<TokenService>;
聽 let authService: jasmine.SpyObj<AuthService>;
聽 let router: jasmine.SpyObj<Router>;

聽 beforeEach(() => {
聽 聽 const tokenServiceSpy = jasmine.createSpyObj('TokenService', ['getToken']);
聽 聽 const authServiceSpy = jasmine.createSpyObj('AuthService', ['user$']);
聽 聽 const routerSpy = jasmine.createSpyObj('Router', ['navigate']);
聽 聽 TestBed.configureTestingModule({
聽 聽 聽 providers: [
聽 聽 聽 聽 AuthGuard,
聽 聽 聽 聽 { provide: TokenService, useValue: tokenServiceSpy },
聽 聽 聽 聽 { provide: AuthService, useValue: authServiceSpy },
聽 聽 聽 聽 { provide: Router, useValue: routerSpy },
聽 聽 聽 聽]
聽 聽 });
聽 聽 guard = TestBed.inject(AuthGuard);
聽 聽 tokenService = TestBed.inject(TokenService) as jasmine.SpyObj<TokenService>;
聽 聽 authService = TestBed.inject(AuthService) as jasmine.SpyObj<AuthService>;
聽 聽 router = TestBed.inject(Router) as jasmine.SpyObj<Router>;
聽 });

聽 it('should be created', () => {
聽 聽 expect(guard).toBeTruthy();
聽 });
});

Cree un user snippet para este tem de los espias:

"spy method":{
      "scope": "typescript",
      "prefix": "test-spyObj",
      "body": [
          "let $1: jasmine.SpyObj<$2>;",
          "const $1Spy = jasmine.createSpyObj('$2', ['$3']);", 
          "{ provide: $2, useValue: $1Spy },",
          "$1 = TestBed.inject($2) as jasmine.SpyObj<$2>"         
        ],
      "description": "spy method"
    },

claro luego hay que ubicar cada linea donde corresponde, pero ahorra mucho tiempo de copiar y pegar