Tuesday, 11 April 2017

Sinon stub check that external SOAP call was called with specific arg

I'm using Jasmine and sinon to test some node.js code. I'm using sinon stubs in this example.

I am trying to stub out a SOAP call to an external service, and I'm wondering if there's any way to ensure that the correct argument is being used in the call.

I have successfully checked if functions are returning the correct arguments in other circumstances, but unfortunately, this scenario is within a nested callback, so I'm not sure how to do it.

Here's my code snippet (I'm trying to test that "client.ExternalService.HttpPort.actualCall" is being called with the "args" variable I am expecting):

class ExternalServiceCaller extends BaseService {
    constructor(util) {
        super(util);
        this.util = util;
    }

    callExternalService(body, callback){
        let url = this.util.config.get('blah.my.url');
        let args = {
            'request':{
                'Property1': body.Property1,
                'Property2': body.Property2,
                'Property3': body.Property3,
                'Property4': body.Property4
            }
        };

        //soap.request()
        soap.createClient(url, sslOptions, function(err, client) {
            //client.[wsdlName].[binding name].[operation]
            client.ExternalService
            .HttpPort
            .actualCall(args, function(err, result) {
                if(!err){
                    callback(null, result);
                }
            }, sslOptions);
        });
    }
}

As I said above, I'm trying to write a test to make sure that actualCall is using the expected "args" variable (making sure that the incoming body is being formatted correctly to be passed to the external call). I can do this pretty easily for the url by stubbing out soap.createClient and using sinon.assert.calledWith() like below:

describe('The function', function(){
let service;
let externalServiceStub;
let externalRequest = helper.myExternalRequestObject;

describe('should use the correct URL',function(){
    beforeEach(function(){
        service = new ExternalServiceCaller(tools);
        externalServiceStub = sinon.stub(soap, 'createClient');
    });
    it ('and uses the correct URL when successful', function(){
        let url = tools.config.get('blah.my.url');
        service.callExternalService(myExternalRequestObject, callback => {});
        sinon.assert.calledWith(externalServiceStub, url);
        externalServiceStub.restore();
    });
});

Unfortunately, I have no idea how to check to see that actualCall is being called with the "args" variable that I'm expecting. I could use a fake object to check it against, but I'm not sure how exactly to do that check in the first place in this scenario.

I looked into soap stub, but there isn't a whole lot of documentation and the example didn't make sense to me.

Any help would be greatly appreciated. :)



via OhGodEverythingIsBroken

No comments:

Post a Comment