1

ループバックを使用してテストを実行しようとしています。このテストには google API の呼び出しが含まれており、Sinon でそれをモックしたいと考えています。

コントローラー:

[...]

コンストラクターで:

  @inject('services.OAuth2Service')
    private oauth2Service: OAuth2Service

[...]

エンドポイントで:

@post('/user-accounts/authenticate/oauth2', {
async authenticateOauth2(
    @requestBody() oauthRequest: OAuthId,
    @inject(RestBindings.Http.REQUEST) _req: Request,
  ): Promise<AccessToken> {
    const email = await this.oauth2Service.getOAuth2User(oauthRequest); // The method to mock.
  ....
}

テスト:

it('oauth2 authentication with google', async () => {

 //Create a spy for the getOAuth2User function
    inject.getter('services.OAuth2Service');
    var oauth2Service: OAuth2Service;

    var setOauthSpy = sinon.spy(oauth2Service, "getOAuth2User"); // Error: Variable 'oauth2Service' is used before being assigned

    const res = await client
      .post('/user-accounts/authenticate/oauth2')
      .set('urlTenant', TEST_TENANT_URL1A)
      .set('userType', TEST_USERTYPE1)
      .send({
        code: TEST_GOOGLE_AUTH2_CODE_KO,
        providerId: TEST_GOOGLE_PROVIDER,
        redirectUri: TEST_GOOGLE_REDIRECT_URI,
      })
      .expect(401);
    expect(res.body.error.message).to.equal('The credentials are not correct.');

    setOauthSpy.restore();

  });

この方法をどのようにテストできますか? ループバックでコンストラクターにインジェクションを含むエンドポイントをテストするにはどうすればよいですか? 助けが必要です。

4

1 に答える 1