These are chat archives for dareid/chakram

9th
Nov 2016
Andrea Leofreddi
@aleofreddi
Nov 09 2016 12:20
hello everybody
I have two api sequential api calls where I need to pass some result from the body response of the first call to the second one. What is a the best way to do that?
Daniel Reid
@dareid
Nov 09 2016 12:27
Hi, I think all of the examples have situations like this https://github.com/dareid/chakram/tree/master/examples
Andrea Leofreddi
@aleofreddi
Nov 09 2016 12:28
Hello Daniel, thanks!
Daniel Reid
@dareid
Nov 09 2016 12:28
for example here https://github.com/dareid/chakram/blob/master/examples/spotify.js#L91 we have two requests being made which we wait for the responses to use in assertions
but instead of assertions you can simply make another calll
Andrea Leofreddi
@aleofreddi
Nov 09 2016 12:33
right, in my case I have a quite long sequential workflow, I was trying to avoid nesting tests with the promises because It ends up unreadable
I would end with like call1.then(function(response1) { call2.then(function(response2). .... } }
so I'm trying to to that with "after"
Daniel Reid
@dareid
Nov 09 2016 12:34
so you can always go something like this:
call1.then(function (resp) {
     return call2
}).then(function (resp) {
    return call3
}).then(function(resp){
   etc. etc.
it depends what you need in scope
Andrea Leofreddi
@aleofreddi
Nov 09 2016 12:37
that looks great! I'll give it a try. Thanks!
Andrea Leofreddi
@aleofreddi
Nov 09 2016 14:39
I've got my test running smoothly with the approach above, but I get <<Error: Some expectation promises were not fulfilled before the test finished. Ensure you are waiting for all the expectations to run>> when I chain more than calls
Daniel Reid
@dareid
Nov 09 2016 14:40
Ah you returning the promise chain from the it test block
Are*
Andrea Leofreddi
@aleofreddi
Nov 09 2016 14:42
yes
Daniel Reid
@dareid
Nov 09 2016 14:43
Could u share the code?
Andrea Leofreddi
@aleofreddi
Nov 09 2016 14:47
yes
it("extend search", function() {
    var token, searchPositionId, destPositionId;

    return Authenticate.run()
            .then(function(response) {
                token = Authenticate.getToken(response);

                console.log('Using token ' + token);

                return Position.run(token, 'Paris');
            })
            .then(function(response) {
                searchPositionId = _.pairs(Position.getPositions(response))[0][0];

                console.log('Using source position #' + searchPositionId);

                return Position.run(token, 'Lille');
            })
            .then(function(response) {
                destPositionId = _.pairs(Position.getPositions(response))[0][0];

                console.log('Using destination position #' + destPositionId);

                return Search.run(token, searchPositionId, destPositionId);
            })
            // .then(function(response) { searchId = Search.getSearch(response).searchId; console.log('Got search #' + searchId); })
        ;
});
I'm trying to put the calls inside objects, like "Authenticate" so I can reuse them for different tests
var Authenticate = {
    run: function() {
        var authenticate = chakram.post("XXX");

        expect(authenticate).to.have.status(200);

        chakram.wait();

        return authenticate;
    },

    getToken: function(response) {
        return response.response.body.access_token;
    }
};
Daniel Reid
@dareid
Nov 09 2016 14:53
Ah I see, so in ur run method there you are waiting for the request but not the assertion, lemme try and rewrite that for you
try out;
var Authenticate = {
    run: function() {
        var authenticate = chakram.post("XXX");

    return authenticate.then(function (resp) {
        expect(resp).to.have.status(200);
        return resp;
    });
    },

    getToken: function(response) {
        return response.response.body.access_token;
    }
};
Andrea Leofreddi
@aleofreddi
Nov 09 2016 14:59
great it works! Daniel thanks a lot for the great tool and great support!
Daniel Reid
@dareid
Nov 09 2016 14:59
ah great, pleased it worked out :)