简体   繁体   English

测试AWS服务无法按预期运行时,我的Node.JS项目中的sinon.spy

[英]sinon.spy in my Node.JS project when testing an AWS service not working as expected

in my Node.JS project (a backend for an Angular 5 project) I have created a service that deals with the AWS Authentication... I have called this awsAuthenticationService . 在我的Node.JS项目(一个Angular 5项目的后端)中,我创建了一个处理AWS Authentication ...的服务,我将其称为awsAuthenticationService All works well but I now need to test it. 一切正常,但我现在需要对其进行测试。 In my awsAuthenticationService.js I have the following method that has some minor logic and then calls a method provided by the " cognitoIdentityServiceProvider ". 在我的awsAuthenticationService.js我具有以下方法,该方法具有一些次要逻辑,然后调用“ cognitoIdentityServiceProvider ”提供的方法。 Here is a snippet of my code (I really have reduced this) 这是我的代码的一部分(我的确减少了)

constructor() {
    this._cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider(this.cognitoConfig);
}

toggleUserAccess(userName, type) {
    const params = {
      Username: userName,
      UserPoolId: this.cognitoConfig.userPoolId
    };

    if (type === null) {
      return this._cognitoIdentityServiceProvider.adminEnableUser(params).promise();
    }
    return this._cognitoIdentityServiceProvider.adminDisableUser(params).promise();
}

As you can see from the toggleUserAccess we pass a few parameters, determine what they are then call the appropriate method. toggleUserAccess可以看到,我们传递了一些参数,确定它们之后调用适当的方法。 I wish to test this by having a unit test that will call the authenticationService.toggleUserAccess , pass some params and spy on the authenticationService._cognitoIdentityServiceProvider methods to see if they were called. 我希望通过进行单元测试来对其进行测试,该单元测试将调用authenticationService.toggleUserAccess ,传递一些参数并监视authenticationService._cognitoIdentityServiceProvider方法以查看是否被调用。 I set it up so... 我将它设置为...

let authenticationService = require('./awsAuthenticationService');

        describe('toggleUserAccess', () => {
        beforeEach(() => {
          authenticationService._cognitoIdentityServiceProvider = {
            adminDisableUser(params) {
              return {
                promise() {
                  return Promise.resolve(params);
                }
              };
            }
          };

          authenticationService._cognitoIdentityServiceProvider = {
            adminEnableUser(params) {
              return {
                promise() {
                  return Promise.resolve(params);
                }
              };
            }
          };
        });

        it('should call adminEnableUser if the type is null', () => {
          authenticationService.toggleUserAccess('TheUser', null);

          const spyCognito = sinon.spy(authenticationService._cognitoIdentityServiceProvider, 'adminEnableUser');
          expect(spyCognito.calledOnce).to.equal(true);
        });

        it('should call adminDisableUser if the type is null', () => {
          authenticationService.toggleUserAccess('TheUser', '0001');

          const spyCognito = sinon.spy(authenticationService._cognitoIdentityServiceProvider, 'adminDisableUser');
          expect(spyCognito.calledOnce).to.equal(true);
        });
      });

My tests aren't passing and I think I have set up my sinon.spy s incorrectly - can anyone see what I am doing wrong or give advice please 我的测试未通过,我认为我的sinon.spy s设置不正确-有人可以看到我做错了还是请提供建议

To stub class of AWS.CognitoIdentityServiceProvider , need to stub with its prototype keyword. 要对AWS.CognitoIdentityServiceProvider类进行AWS.CognitoIdentityServiceProvider ,需要对其prototype关键字进行存根。

// add require statement for your AWS class    

const spyCognito = sinon.spy(AWS.CognitoIdentityServiceProvider.prototype, 'adminDisableUser');
expect(spyCognito.calledOnce).to.equal(true);

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM