角度测试:服务异步函数错误的单元测试

问题描述 投票:0回答:0

我的 Angular 单元测试服务有一个我不明白的错误.

auth.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';

import { UserService } from './user.service';
import { User } from '../shared/models/user.model';

@Injectable()
export class AuthService {
  constructor(
    private userService: UserService,
    private http: HttpClient,
    private router: Router,
  ) {
  }

  convertToken(username: string, password: string): Observable<any> {
    let body = {
      grant_type: 'password',
      client_id: environment.django_client_id,
      client_secret: environment.django_client_secret,
      backend: 'django',
      username: username,
      password: password,
    };

    return this.http.post(environment.api_url + '/auth/token/', body);
  }
}

auth.service.spec.ts

/* tslint:disable:no-unused-variable */
import { HttpClient } from '@angular/common/http';
import {
  HttpClientTestingModule,
  HttpTestingController,
} from '@angular/common/http/testing';
import {
  TestBed,
  fakeAsync,
  inject,
  ComponentFixture,
  tick,
  flush,
} from '@angular/core/testing';
import { Router } from '@angular/router';
import { ToastComponent } from '../components/toast/toast.component';
import { AuthService } from './auth.service';
import { UserService } from './user.service';
import { User } from '../shared/models/user.model';
import { RouterTestingModule } from '@angular/router/testing';
import { Observable, of } from 'rxjs';
import { environment } from 'src/environments/environment';

describe('AuthService', () => {
  let authService: AuthService;
  let userService: UserService;
  let http: HttpClient;
  let httpTestingController: HttpTestingController;

  let token = '';
  let fixture: any;

  beforeEach(() => {
    //Configures testing app module
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, RouterTestingModule],
      providers: [AuthService, UserService, HttpClient, ToastComponent],
    });

    //Instantiates HttpClient, HttpTestingController and EmployeeService
    http = TestBed.inject(HttpClient);
    httpTestingController = TestBed.inject(HttpTestingController);
    authService = TestBed.inject(AuthService);
    userService = TestBed.inject(UserService);
    spyOn(http, 'post').and.returnValue(of({}));
  });

  afterEach(() => {
    httpTestingController.verify(); //Verifies that no requests are outstanding.
  });

  it('should login user', fakeAsync(() => {
    let token_type = '';
    authService
      .convertToken('username', 'password')
      .subscribe((res: any) => (token_type = res.token_type));

    tick(1000);

    expect(token_type).toEqual('Bearer');
  }));

  describe('generate new token', () => {
    it('should return all products', fakeAsync(
      inject([AuthService], (authService: AuthService) => {
        let token_type: any;

        authService
          .convertToken('username', 'password')
          .subscribe((result) => {
            token_type = result;
          });

        tick();

        expect(token_type).toEqual('Bearer');
      })
    ));
  });
});

在这两个描述的步骤中,由于未定义“token_type”变量,我的断言处于error。我从未在我的订阅结果中看到任何结果,但我认为它应该可以正常工作。

你知道我的单元测试为什么出错吗?

Expected undefined to equal 'Bearer'.
    at <Jasmine>
    at UserContext.apply (http://localhost:9876/_karma_webpack_/webpack:/src/app/services/auth.service.spec.ts:99:24)
    at UserContext.apply (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone-testing.js:2013:30)
    at _ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:372:26)

我认为它来自 SpyOn 但我真的不知道我可以传递给 returnValue 因为我真的不知道返回内容。

谢谢。

angularjs typescript unit-testing karma-jasmine angular-test
© www.soinside.com 2019 - 2024. All rights reserved.