【发布时间】:2019-03-12 12:13:24
【问题描述】:
谁能帮帮我?我正在尝试测试一个调用 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);
});
});
}
这是我第一次测试jasmine,我学习了,但我不知道如何解决这个问题,如何调用异步函数并获得返回。
【问题讨论】:
-
你检查过构造函数中是否提供了 authService 吗?
-
@jakubm 现在我正在使用 component = fixture.componentInstance;,并且正在工作,这就是问题所在,没有 authservice 的提供者,但我的异步功能不起作用,我用它调用('', async((done) => { // 履行我的承诺...但是我的测试先执行,然后是我的承诺,我该如何解决?}))
-
你能更新你的问题和代码sn-ps,我会更容易看到发生了什么。
-
@jakubm 我解决了我的问题,我发布了我的答案,谢谢大家!
标签: javascript angular testing jasmine