如何在Jasmine中正确模拟Amplify进行单元测试? [英] How do I properly mock Amplify for Unit Testing in Jasmine?

查看:84
本文介绍了如何在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进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆