2016-12-19 22 views
9

Bu tür bir soru sorduğum için üzgünüm. Ancak canActivate koruma dosyası testini yazarken herhangi bir blog veya youtube öğreticisi bulamıyorum. Resmi belgelerde de belirtilen bir şey yoktur.Nasıl birim-test Jasmine kullanarak angular2 koruma yöntemini etkinleştirebilir?

herhangi bir yardım çok takdir edilecektir. Bu soruyu yanıtlayabilen kişilere yardımcı olmak için, kod snippet'ımı referans için yapıştırıyorum,

+0

tam olarak ne test etmek istersiniz? Koruyucunuzdaki canActivate() 'yönteminin gerçekten çağrıldığı veya" canActivate() "yönteminin içindeki kod olduğu? – AngularChef

cevap

14

sampleLoggedIn.guard.ts

import {Injectable} from '@angular/core'; 
import {Router, CanActivate} from '@angular/router'; 
import {StorageService} from '../storage.service'; 

@Injectable() 
export class LoggedInGuard implements CanActivate { 
    constructor(private router: Router, private storageService: StorageService) { 
    } 

    /**Overriding canActivate to guard routes 
    * 
    * This method returns true if the user is not logged in 
    * @returns {boolean} 
    */ 
    canActivate() { 
     if (this.storageService.isLoggedIn) { 
      return true; 
     } else { 
      this.router.navigate(['home']); 
      return false; 
     } 
    } 
} 

sampleLoggedIn.guard.spec.ts

import {TestBed, async} from '@angular/core/testing'; 
import {FormsModule} from '@angular/forms'; 
import {HttpModule} from '@angular/http'; 
import {CommonModule} from '@angular/common'; 
import 'rxjs/Rx'; 
import 'rxjs/add/observable/throw'; 
import {Router} from '@angular/router'; 
import 'rxjs/add/operator/map'; 
import {LoggedInGuard} from './loggedin.guard'; 
import {StorageService} from '../storage.service'; 
import {CookieService} from 'angular2-cookie/core'; 

describe('Logged in guard should',() => { 
    let loggedInGuard: LoggedInGuard; 
    let storageService: StorageService; 
    let router = { 
     navigate: jasmine.createSpy('navigate') 
    }; 

    // async beforeEach 
    beforeEach(async(() => { 
     TestBed.configureTestingModule({ 
      imports: [FormsModule, CommonModule, HttpModule], 
      providers: [LoggedInGuard, StorageService, CookieService, 
       {provide: Router, useValue: router} 
      ] 
     }) 
      .compileComponents(); // compile template and css 
    })); 

    // synchronous beforeEach 
    beforeEach(() => { 
     loggedInGuard = TestBed.get(LoggedInGuard); 
     storageService = TestBed.get(StorageService); 
    }); 

    it('be able to hit route when user is logged in',() => { 
     storageService.isLoggedIn = true; 
     expect(loggedInGuard.canActivate()).toBe(true); 
    }); 

    it('not be able to hit route when user is not logged in',() => { 
     storageService.isLoggedIn = false; 
     expect(loggedInGuard.canActivate()).toBe(false); 
    }); 
}); 
+0

canActivate() argümanlar gerektirir. Bunların nasıl sağlanacağıyla ilgili [bu soru] (https://stackoverflow.com/questions/40870670/how-do-i-mock-routerstatesnapshot-for-a-router-guard-jasmine-test) adresine bakın. –