如何使用谷歌提供商登录单元测试Angularfire2(版本5)auth服务

时间:2018-02-05 21:47:43

标签: unit-testing firebase karma-jasmine angularfire2 angular5

我尝试使用AngularFire2(version5)google provider登录设置Angular5应用程序的单元测试,我的auth服务非常简单,它看起来像这样:

let authState = null;
let mockAngularFireAuth: any = {authState: Observable.of(authState)};

@Injectable()
export class AuthService {
  loggedIn: boolean;
  private user: Observable<firebase.User>;

  constructor(
          public afAuth: AngularFireAuth
  ) {
    this.user = afAuth.authState;
    this.user.subscribe(
      (user) => {
       if (user) {
          this.loggedIn = true;
       } else {
          this.loggedIn = false;
       }
    });
}

  // --------------------------------- Google Login -----------------------------------

  loginWithGoogle() {
    // Sign in/up with google provider
    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(() => {
    this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider())
      .catch((error) => {
        if (error.code === 'auth/account-exists-with-different-credential') {
          alert('This email address is already registered');
        }
      });
  });
  }

  // ------------------------- Checks User Authentication -----------------------

  isAuthenticated() {
    // returns true if the user is logged in
    return this.loggedIn;
  }


  // --------------------------------- User LogOut -----------------------------------

  logOut() {
    this.afAuth.auth.signOut()
      .then(() => {
        this.loggedIn = false;
     });
 }
 }

我想测试我的loginWithGoogle()方法,但我不知道从哪里开始。到目前为止,我的auth服务规范文件如下所示:

describe('AuthService', () => {
  let authService;
  beforeEach(() => {
   TestBed.configureTestingModule({
      imports: [
      AngularFireDatabaseModule,
      AngularFireModule.initializeApp(environment.firebase),
      RouterTestingModule
      ],
     providers: [
       {provide: AngularFireAuth, useValue: mockAngularFireAuth},
       AuthService,
     ]
  });

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

 it('should be defined', () => {
    expect(authService).toBeDefined();
 });

 it('should return true if loggedIn is true', () => {
    expect(authService.isAuthenticated()).toBeFalsy();
    authService.loggedIn = true;
    expect(authService.isAuthenticated()).toBeTruthy();
 });
});

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

嗯,这就是我的所作所为。我嘲笑AngularFireAuth并以拒绝或解决承诺的方式返回承诺。我是茉莉和测试的新手,如果我做错了,请随时纠正我。

it('should return a rejected promise', () => {
authState = {
  email: 'lanchanagupta@gmail.com',
  password: 'password',
};

mockAngularFireAuth = {
  auth: jasmine.createSpyObj('auth', {
    'signInWithPopup': Promise.reject({
      code: 'auth/account-exists-with-different-credential'
    }),
  }),
  authState: Observable.of(authState)
};
mockAngularFireAuth.auth.signInWithPopup()
  .catch((error: { code: string }) => {
    expect(error.code).toBe('auth/account-exists-with-different-credential');
  });
  });


it('should return a resolved promise', () => {
authState = {
  email: 'lanchanagupta@gmail.com',
  password: 'password',
  uid: 'nuDdbfbhTwgkF5C6HN5DWDflpA83'
};

mockAngularFireAuth = {
  auth: jasmine.createSpyObj('auth', {
    'signInWithPopup': Promise.resolve({
      user: authState
    }),
  })
};
mockAngularFireAuth.auth.signInWithPopup()
  .then(data => {
    expect(data['user']).toBe(authState);
  });
 });