assaf / node-replay

When API testing slows you down: record and replay HTTP responses like a boss
http://documentup.com/assaf/node-replay
MIT License
522 stars 107 forks source link

Is there a way to know a certain stubbed service was called? #129

Closed zhangchiqing closed 7 years ago

zhangchiqing commented 7 years ago

I found this library is super handy for stubbing external services in my tests. But there is one thing missing (not sure if it already exists) is that how can I verify the stubbed service is being called? Further I'd like to check if was called with the right parameters.

Does this functionality already exist? Or is there workaround to do this?

Thanks!

zhangchiqing commented 7 years ago

Maybe it could be an optional to add and remove a callback with addCallback and removeCallback, so I can use them like this to verify if a service is being called with certain arguments.

var replay = require('replay');
var myservice = require('./myservice');

describe('test service', function() {
  beforeEach(function() {
     var calledWith = [];
     this.calledWith = calledWith;
     this.callback = function(called) {
        calledWith.push(called);
     };
     replay.addCallback(this.callback);
  });
  afterEach(function() {
    replay.removeCallback(this.callback);
  });

  it('should called with data', function() {
    var calledWith = this.calledWith;
    return myservice.getData({ id: 123 }).then(function() {
      calledWith[0].url.contains('id=123');
    });
  });
});
zhangchiqing commented 7 years ago

I found a workaround with sinon. Closing. Thanks