Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to spy on superagent with Jasmine?

I'm using the superagent ajax library for an app, and I'm trying to write some unit tests for it. I've got a class which looks like this:

someClass = {
  getData: function(){
    _this = this;
    superagent.get('/some_url').end(function(res){
      if(res.body){
        _this.data = res.body
      }
     });
   });
 }

How do I write a Jasmine test to spy on the _this.data = res.body call? Setting up a spy with and.callThrough() on getData isn't working. I don't want to actually call the URL in question; I'm just trying to test that if it gets data, it does something with it.

Thanks

like image 644
Kevin Whitaker Avatar asked Dec 18 '14 19:12

Kevin Whitaker


Video Answer


1 Answers

spyOn(superagent, 'get').and.callFake(function(url) {
  return {
    end: function(cb) {
      //null for no error, and object to mirror how a response would look.
      cb(null, {body: data});
    }
  }
});
like image 190
Bror Avatar answered Sep 27 '22 20:09

Bror