【问题标题】:Jasmine - How to write test case for AWS service authenticateUser?Jasmine - 如何为 AWS 服务 authenticateUser 编写测试用例?
【发布时间】:2018-03-19 11:31:44
【问题描述】:

我在 UI 级别使用 Angular 代码,并希望使用 Jasmine 测试以下组件代码:

this.poolData = {
  UserPoolId: '<UserPoolId>',
  ClientId: '<ClientId>'
};
const userPool = new AWSCognito.CognitoUserPool(this.poolData);
const authDetails = new AWSCognito.AuthenticationDetails({
  Username: this.username,
  Password: this.password
});
const cognitoUser = new AWSCognito.CognitoUser({
  Username: this.username,
  Pool: userPool
});
cognitoUser.authenticateUser(authDetails, {
  onSuccess: (result) => {
    this.cognitoIdToken = result.getIdToken().getJwtToken();
  },
  onFailure: (err) => {
    alert('Invalid Username and/or Password');
    return;
  }
});

如何为此编写测试用例?

【问题讨论】:

    标签: angular karma-jasmine


    【解决方案1】:

    你没有。

    您应该测试您的功能是否按预期运行,而不是测试库是否运行良好。那不是你的工作。

    你必须做的是模拟你的依赖。看来您直接使用该库,因此请为此使用间谍。

    import * as AWS from 'your-dependency';
    
    it('should create a Cognito User Pool', () => {
      spyOn(AWS.AWSCognito, 'CognitoUserPool');
    
      feature.myMethod();
    
      expect(AWS.AWSCognito.CognitoUserPool).toHaveBeenCalledWith(feature.poolData);
    });
    

    剩下的交给你,这只是一个例子。

    如果你需要返回一些东西,例如当你创建一个对象时,你可以像这样模拟它:

    spyOn(AWS.AWSCognito, 'CognitoUser').and.returnValue({
      authenticateUser: (details, success, failure) => null
    });
    

    【讨论】:

    • 我是 Jasmine 的新手。请详细说明并给出答案。
    • 我做了详细说明,我做了“给出答案”。你现在应该测试它。
    • 感谢您的回答。我添加了这段代码。我收到错误::CognitoUser 未声明为可写或没有设置器。请建议如何解决。
    • Object.assignProperty(AWS.AWSCognito, 'CognitoUser', { writable: true, value: {}})
    • 我使用 'your-dependency' 作为 'amazon-cognito-identity-js',添加后显示 tslint 错误为 - 类型 'typeof'amazon-cognito 上不存在属性'assignProperty' -identity-js"'
    猜你喜欢
    • 1970-01-01
    • 2015-09-18
    • 2019-05-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多