如何在Jasmine中正确模拟Amplify进行单元测试?

编程入门 行业动态 更新时间:2024-10-25 02:24:14
本文介绍了如何在Jasmine中正确模拟Amplify进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对单元测试还很陌生,所以请耐心等待

I'm quite new to Unit testing so bear with me

我正在尝试对在Angular应用程序中使用Amplify登录用户的服务进行单元测试.

I'm trying to unit test a service that is used to log the user with Amplify in an Angular application.

现在在我正在运行的规格文件中:

Right now in the spec file I'm doing:

beforeEach(async () => { TestBed.configureTestingModule({ imports: [ HttpClientTestingModule, ], providers: [ MyService, Amplify ] } myService = TestBed.get(MyService) amplify = TestBed.get(Amplify) }) it('should login', async () => { const objToBeReturned = { signInUserSession: { idToken: { jwtToken: 'tokenValue' } } } spyOn(Amplify.Auth, 'signIn').and.returnValue(objToBeReturned) await myService.login('username', 'password') })

在 MyService 中:

public async login(username: string, password: string) { const authUser = await Amplify.Auth.signIn(username.toLowerCase(), password) if (authUser.signInUserSession != null) { const idToken = authUser.signInUserSession.idToken.jwtToken return this.patientLogin(idToken) } } private async patientLogin(idToken?: string): Promise<boolean> { await this.sendRequest<LoginResponse>(url, data).pipe( tap(response => { if (!isLoginResponse(response)) { throw throwErr({ code: 'Generic' }) } this.token = response.token }) ).toPromise() return true }

这给了我错误异步功能未在5000毫秒内完成

我很确定这取决于我模拟Amplify的方式

I'm pretty sure it depends on the way I'm mocking Amplify

我该如何正确模拟它?

推荐答案

尝试:

it('should login', async (done) => { spyOn(myService, 'patientLogin'); // assuming it is a public function const objToBeReturned = { signInUserSession: { idToken: { jwtToken: 'tokenValue' } } } // Promise.resolve is optional but since it is returning a promise and we are awaiting it, I think we should do it here as well. spyOn(Amplify.Auth, 'signIn').and.returnValue(Promise.resolve(objToBeReturned)); console.log('calling login'); await myService.login('username', 'password'); // fixture.whenStable() can be optional as well, but I think it will be good to wait for all promises to finish console.log('login resolved'); await fixture.whenStable(); expect(myService.patientLogin).toHaveBeenCalledWith('tokenValue'); // call the done function to tell the test you are done, I think this is what you were missing done(); })

=======编辑=============

====== Edit ===============

您必须找出测试卡在哪里,我认为this.patientLogin(idToken)是异步的并且卡在那里.查看console.logs,确保您看到login resolved.基于这种预感,我监视了patientLogin,并断言它已被调用.

You have to find out where the test is getting stuck, I am thinking this.patientLogin(idToken) is asynchronous and is getting stuck there. Look at the console.logs, make sure you see login resolved. Based on this hunch, I have spied on patientLogin, and just assert it was called.

更多推荐

如何在Jasmine中正确模拟Amplify进行单元测试?

本文发布于:2023-10-16 11:52:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   正确   如何在   Jasmine   Amplify

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!