简体   繁体   English

如何在茉莉花中测试功能

[英]how to test a function in jasmine

can anyone help me please? 有人可以帮我吗? I'm trying to test a function that call a firebase functions, but when I call the main function and start to run a firebase functions, I got a error 我正在尝试测试调用Firebase函数的函数,但是当我调用main函数并开始运行Firebase函数时,出现错误

err TypeError: Cannot read property 'emailPasswordLoginAsPromise' of null

i don't know what is happen, follow my code: 我不知道会发生什么,请遵循我的代码:

 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) }) }); }); 

my Function that I'm calling: 我正在调用的函数:

 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') }); }) } 

in my authService, my emailloginasPromise is like that : 在我的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); }); }); } 

it's my first time with testing jasmine, I studied, but i don't know how I can solve this problem, how call a async func and getting the return. 我学习了这是我第一次测试茉莉花,但是我不知道如何解决这个问题,如何调用异步函数并获得回报。

i founded the problem, follow the fix: 我发现了问题,请解决此问题:

The authService isn't provide when i'm creating a stance of my class, so now i'm using the component: 当我创建类立场时不提供authService,因此现在我正在使用组件:

component = fixture.componentInstance;

with this component now I'm calling my method and all providers is working. 现在,使用此组件,我正在调用我的方法,所有提供程序都在工作。

Follow my describe: 按照我的描述:

 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(); }); }); 

and how to test this component? 以及如何测试该组件?

I'm using the follow tests: 我正在使用以下测试:

 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(); })); 

My class follow: 我的课程如下:

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

and my authService: 和我的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); }); }); } 

And now all my testing is working with asynchronous method with firebase methods 现在我所有的测试都在使用带有Firebase方法的异步方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM