簡體   English   中英

如何在茉莉花中測試功能

[英]how to test a function in jasmine

有人可以幫我嗎? 我正在嘗試測試調用Firebase函數的函數,但是當我調用main函數並開始運行Firebase函數時,出現錯誤

err TypeError: Cannot read property 'emailPasswordLoginAsPromise' of null

我不知道會發生什么,請遵循我的代碼:

 fdescribe('UserLoginContentComponent', () => { let component: UserLoginContentComponent; let fixture: ComponentFixture<UserLoginContentComponent>; let loginComponent = new UserLoginContentComponent(null,null,null,null,null,null,null); beforeAll( async(() => { TestBed.configureTestingModule({ imports: [ SharedModule, AngularFireModule.initializeApp(environment.firebase), RouterTestingModule, BrowserAnimationsModule ], declarations: [UserLoginContentComponent], providers: [ AuthService, AngularFireAuth, AngularFirestore, LogService, LogPublishersService, HttpClient, HttpHandler ] }).compileComponents(); fixture = TestBed.createComponent(UserLoginContentComponent); component = fixture.componentInstance; fixture.detectChanges(); spyOn(loginComponent, 'onSubmit').and.callThrough(); loginComponent.loginModel.email = 'correct email'; loginComponent.loginModel.password = 'correct password'; }) ); it('component should be create', () => { expect(component).toBeTruthy(); }); it('Correct login',function(){ loginComponent.onSubmit().then((x) => { console.log('ok:',x) //expect something .. }).catch((err) => { console.log('err',err) }) }); }); 

我正在調用的函數:

 onSubmit() { //i'm setting my loginModel in the test with email and password console.log('this.loginModel',this.loginModel) return new Promise((res,rej) => { this.authService.emailPasswordLoginAsPromise(this.loginModel).then(userCredential => { // do something.. this.authService.createOrUpdateUserDataFirestore(userCredential, null, avaliacaoChecklist, null, null).then(s => //updating my user or create one }).catch(e => { //catch if this doesn't update or create }); }); res('login OK') }).catch(e => { //open a diaglog if happen something wrong... rej('login Fail') }); }) } 

在我的authService中,我的emailloginasPromise就像這樣:

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

我學習了這是我第一次測試茉莉花,但是我不知道如何解決這個問題,如何調用異步函數並獲得回報。

我發現了問題,請解決此問題:

當我創建類立場時不提供authService,因此現在我正在使用組件:

component = fixture.componentInstance;

現在,使用此組件,我正在調用我的方法,所有提供程序都在工作。

按照我的描述:

 import { ComponentFixture, TestBed } from '@angular/core/testing'; import { SharedModule } from '../../shared/shared.module'; import { UserLoginContentComponent } from './user-login-content.component'; import { AngularFireModule } from '@angular/fire'; import { environment } from 'src/environments/environment'; import { RouterTestingModule } from '@angular/router/testing'; import { AuthService } from 'src/app/core'; import { AngularFireAuth } from '@angular/fire/auth'; import { AngularFirestore } from '@angular/fire/firestore'; import { LogService } from 'src/app/shared/logger/log.service'; import { LogPublishersService } from 'src/app/shared/logger/log-publishers.service'; import { HttpClient, HttpHandler } from '@angular/common/http'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; fdescribe('UserLoginContentComponent', () => { let component: UserLoginContentComponent; let fixture: ComponentFixture<UserLoginContentComponent>; beforeAll(function(){ TestBed.configureTestingModule({ imports: [ SharedModule, AngularFireModule.initializeApp(environment.firebase), RouterTestingModule, BrowserAnimationsModule ], declarations: [UserLoginContentComponent], providers: [ AuthService, AngularFireAuth, AngularFirestore, LogService, LogPublishersService, HttpClient, HttpHandler ] }).compileComponents(); fixture = TestBed.createComponent(UserLoginContentComponent); component = fixture.componentInstance; fixture.detectChanges(); }); }); 

以及如何測試該組件?

我正在使用以下測試:

 it('COrrect login',(async(done) => { component.loginModel.email = 'correctemail@gmail.com'; component.loginModel.password = 'correctpassword'; await component.onSubmitTest().then((x) => { expect(x).toBe('login OK'); }); done(); })); it('Wrong login (email)',(async(done) => { component.loginModel.email = 'wrongemail@gmail.com'; component.loginModel.password = 'correctpassword'; await component.onSubmitTest().then(() => {}) .catch((err) => { expect(err).toBe('login Fail'); }) done(); })); 

我的課程如下:

 onSubmitTest() { return new Promise((res,rej) => { this.authService.emailPasswordLoginAsPromise(this.loginModel).then(() => { res('login OK') }).catch(e => { rej('login Fail') }); }) } 

和我的authService:

 emailPasswordLoginAsPromise(login) { return new Promise((resolveEPL, rejectEPL) => { this.angularFireAuth.auth.signInWithEmailAndPassword(login.email, login.password) .then(credential => { resolveEPL(credential.user); }).catch(e => { rejectEPL(e); }); }); } 

現在我所有的測試都在使用帶有Firebase方法的異步方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM