Passing function as response body

35 views
Skip to first unread message

Audrey Becker

unread,
Jul 5, 2017, 2:50:23 PM7/5/17
to Sinon.JS
Hi,

We have a test case where we want to have sinon use query parameters as arguments for modifying the response body. Specifically, we pass result range (startIndex,endIndex) as query params, and want to return that specific slice of a result array. It seems like the respondWith only allows fixed objects as response and not functions.

Ideally, the code would look like this:

            var startIndex = 0;
            var iterativeResponse = function(){
              var body = {
                'resources' : mockUsers.slice(startIndex, startIndex+2),
                'startIndex' : startIndex,
                'totalResults' : mockUsers.length
              };
              console.log(startIndex);
              startIndex += 2;
              return JSON.stringify(body);
            };
            server.respondWith('GET', /\/users\?.*/, [200, responseHeaders.json, iterativeResponse]);


Is there a way around this problem?

Thanks,
Audrey

Tomas Diaz

unread,
Oct 12, 2017, 4:20:56 PM10/12/17
to Sinon.JS
Hey, did you manage to handle this? I am trying to use a function as well but it doesn't seem to work at all... 

I was actually trying to access the request object during the response to check something with its values. I ended up checking them through the requests array from the server object but I was wondering why a function in the response parameter wouldn't work.

Cheers!
Reply all
Reply to author
Forward
0 new messages