用茉莉花测试AngularFireAuth的功能

时间:2019-03-08 17:06:27

标签: javascript node.js angular testing jasmine

你好吗? 这是我第一次用茉莉花,我迷路了。 我只想测试我的AngularFireAuth方法。

我在服务中拥有此功能:

emailPasswordLoginAsPromise(login) {

    return new Promise((resolveEPL, rejectEPL) => {

      this.angularFireAuth.auth.signInWithEmailAndPassword(login.email, login.password)
        .then(credential => {
          resolveEPL(credential.user);
        }).catch(e => {
          console.error('emailPasswordLogin', e);
          rejectEPL(e);
        });
    });
  }

并且我想在茉莉花中进行测试,而我的Spec.js中有这个:

我知道模拟的基本知识,但是我不知道如何在测试中应用它,有人可以帮我吗?我试图找到一些例子,但是我很困惑。

import { TestBed, inject } from '@angular/core/testing';
import { AuthService } from './auth.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFireModule } from '@angular/fire';
import { environment } from 'src/environments/environment';
import { RouterTestingModule } from '@angular/router/testing';

fdescribe('AuthService', () => {

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        AngularFireModule.initializeApp(environment.firebase),
        RouterTestingModule
      ],
      providers: [ 
        AuthService, AngularFireAuth, AngularFirestore 
      ]
    });
  });


  it('login correct', inject([AuthService], (service: AuthService) => {

    spyOn(service,'emailPasswordLoginAsPromise').and.returnValue(Promise.resolve());

    let login = {
      email: 'email@gmail.com',
      password: 'correctpassword'
    }
      expect(service.emailPasswordLoginAsPromise(login)).toEqual(jasmine.any(Promise));
  }));

  it('login incorret', inject([AuthService], (service: AuthService) => {

    spyOn(service,'emailPasswordLoginAsPromise').and.returnValue(Promise.reject());

    let login = {
      email: 'email@gmail.com',
      password: 'wrongpassword'
    }
      expect(service.emailPasswordLoginAsPromise(login)).toEqual(jasmine.any(Promise));
  }));

});

0 个答案:

没有答案