简体   繁体   中英

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 . 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 ". 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. 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. 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

To stub class of AWS.CognitoIdentityServiceProvider , need to stub with its prototype keyword.

// add require statement for your AWS class    

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

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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