如何在构造函数中使用可观察的订阅对角度卫队进行单元测试

时间:2019-03-29 16:04:34

标签: angular unit-testing karma-jasmine angular-services

我正在尝试对角度防护装置进行单元测试,该防护装置通过管道传递属于身份验证服务的可观察物。订阅通过警卫canActivate()方法进行。

我在身份验证服务上使用了茉莉花间谍程序,可以观察到返回值,但是在我的单元测试中从未调用过该间谍程序。

在测试组件时,我使用fixture.detectChanges(),但是对于这种保护措施,我无法找到一种方法来测试它是否根据可观察的值返回正确的值。

auth-guard.ts:

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor(
    private authService: AuthService,
    private router: Router
  ) {}

  canActivate(): Observable<boolean> {
    return this.authService.isAuthenticated$.pipe(
      map(e => {
        if (e) {
          return true;
        }
      }),
      catchError((err) => {
        this.router.navigate(['/login']);
        return of(false);
      })
    );
  }
}

auth.service.ts:

@Injectable()
export class AuthService {

  private isAuthenticated = new BehaviorSubject<boolean>(false);

  get isAuthenticated$(): Observable<boolean> { return this.isAuthenticated.asObservable(); }

  ...
}

auth-guard.spec.ts:

describe('Authuard', () => {

  let authGuard: AuthGuard;
  let authService: jasmine.SpyObj<AuthService>;

  beforeEach(() => {

    const authServiceSpy = jasmine.createSpyObj('AuthService', ['isAuthenticated$']);

    TestBed.configureTestingModule({
      providers: [
        AuthGuard,
        { provide: AuthService, useValue: authServiceSpy }
      ]
    });

    authGuard = TestBed.get(AuthGuard);
    authService = TestBed.get(AuthService);
  });

  it('should create', () => {
    expect(authGuard).toBeDefined();
  });

  /* This test fails */
  it('should return false when not authenticated', () => {
    authService.isAuthenticated$.and.returnValue(of(false));

    authGuard.canActivate().subscribe(canActivate => {
        expect(canActivate).toBe(false);
    });
  });
});

第二项测试失败,并显示this.authService.isAuthenticated$.pipe is not a function。间谍在isAuthenticated $上返回的值不被采用。

当身份验证服务返回的可观察值发生更改时,如何测试防护器是否返回了正确的值?茉莉花间谍可以做到吗?

1 个答案:

答案 0 :(得分:0)

按如下所示尝试throwError

import { throwError } from 'rxjs';

it('should return false when not authenticated', () => {
    spyOn(authService,'isAuthenticated$').and.returnValue(throwError('error'));

    authGuard.canActivate().subscribe(canActivate => {
        expect(canActivate).toBeFalsy();
});
相关问题