【问题标题】:Unit Test Mock Rejected Promise in Angular2Angular2中的单元测试模拟拒绝承诺
【发布时间】:2017-08-01 15:33:33
【问题描述】:

我正在尝试测试 Angular2 身份验证服务 (authService),特别是模拟 AngularFireAuth 提供程序,以便我可以通过监视和返回 AngularFireAuth 方法的值来控制和测试各种身份验证场景,例如signInAnonymously.

以下是我的测试规范的摘录。你可以看到最初的 authState 是一个 null 的 Observable——我们没有经过身份验证。然后authService 应该尝试匿名身份验证(使用signInAnonymously)。

AngularFire 中,signInAnonymously 方法返回一个承诺。我希望这被拒绝并捕获错误。

这只是一次场景,但如果我能解决这个问题,我可能会解决剩下的问题。

auth.service.spec.ts

import { inject, TestBed } from '@angular/core/testing';

import { AngularFireAuth } from 'angularfire2/auth';
import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Rx';

import { AuthService } from './auth.service';
import { MockUser } from './testing/mock-user';
import { environment } from '../../environments/environment';

let authState: MockUser;
let mockAngularFireAuth: any = {
  auth: jasmine.createSpyObj('auth', [
    'signInAnonymously',
    'signInWithPopup',
    'signOut'
  ]),
  authState: Observable.of(null)
};
let service: AuthService;

fdescribe('AuthService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: AngularFireAuth, useValue: mockAngularFireAuth },
        { provide: AuthService, useClass: AuthService }
      ]
    });
  });

  beforeEach(inject([ AuthService ], (authService: AuthService) => {
    service = authService;
  }));

  // TODO: Test `authState`  is `null` and throw
  describe('when we can’t authenticate', () => {
    it('should thow', () => {
      mockAngularFireAuth.auth.signInAnonymously.and.returnValue(() => {
        return new Promise((resolve, reject) => {
          reject('fooBar');
        });
      });
    });
  });
});

auth.service.ts

import { Injectable } from '@angular/core';

import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class AuthService {
  private authState: firebase.User;

  constructor(private afAuth: AngularFireAuth) { this.init(); }

  private init(): void {
    this.afAuth.authState.subscribe((authState) => {
      console.log(1, authState);

      if (authState === null) {
        console.log(2, authState);

        this.afAuth.auth.signInAnonymously()
          .then((authState) => {
            console.log(3, authState);

            this.authState = authState;
          })
          .catch((error) => {
            console.log(4, error.message);

            throw new Error(error.message);
          });
      } else {
        console.log(5, authState);

        this.authState = authState;
      }
    }, (error) => {
      console.log(6, error.message);

      throw new Error(error.message);
    });
  }
}

我得到的错误是Cannot read property 'then' of undefinedauth.service.ts 的第 21:11 行;正如您所期望的那样,.then((authState) => {... 行。

【问题讨论】:

    标签: angular unit-testing typescript jasmine angularfire2


    【解决方案1】:

    AuthService 在模拟 signInAnonymously 之前被实例化。

    相反,它可以是

      describe('when we can’t authenticate', () => {
        beforeEach(() => {
          mockAngularFireAuth.auth.signInAnonymously.and.returnValue(Promise.reject('fooBar'));
        });
    
        it('...', inject([ AuthService ], (authService: AuthService) => {
          ...
    

    【讨论】:

    • 啊,我想当我创建间谍对象时,它可能会在以后被劫持。我会尽快试试这个,谢谢
    猜你喜欢
    • 2020-11-21
    • 1970-01-01
    • 2017-10-21
    • 2015-03-15
    • 1970-01-01
    • 2014-05-14
    • 2018-02-25
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多