strongloop-archive / loopback-testing

DEPRECATED Utilities for testing LoopBack apps
Other
8 stars 20 forks source link

withArgs() does not include data in http request #60

Closed vanraizen closed 9 years ago

vanraizen commented 9 years ago

I'm trying to write a test case that passes arguments into a POST:

lt.beforeEach.withApp(app);
lt.beforeEach.withArgs({key1: value1, key2: value2, key3: value3});

But the arguments never get included with any HTTP request.

In the code it is accepting args here:

_beforeEach.withArgs = function() {
  var args = Array.prototype.slice.call(arguments, 0);
  beforeEach(function() {
    this.args = args;
  });
}

But when a remote is made there is no use of this.args anywhere:

_describe.whenCalledRemotely = function(verb, url, data, cb) {
  if (cb == undefined) {
    cb = data;
    data = null;
  }

  var urlStr = url;
  if(typeof url === 'function') {
    urlStr = '/<dynamic>';
  }
  else if(typeof url === 'object' && url.hasOwnProperty('placeHolder')) {
    urlStr = url.placeHolder;
  }

  describe(verb.toUpperCase() + ' ' + urlStr, function() {
    beforeEach(function(cb) {
      if(typeof url === 'function') {
        this.url = url.call(this);
      }
      else if(typeof url === 'object' && url.hasOwnProperty('callback')){
        this.url = url.callback.call(this);
      }
      this.remotely = true;
      this.verb = verb.toUpperCase();
      this.url = this.url || url;
      var methodForVerb = verb.toLowerCase();
      if(methodForVerb === 'delete') methodForVerb = 'del';

      if (this.request === undefined) {
          throw new Error('App is not specified. Please use lt.beforeEach.withApp to specify the app.');
      }

      this.http = this.request[methodForVerb](this.url);
      delete this.url;
      this.http.set('Accept', 'application/json');
      if(this.loggedInAccessToken) {
        this.http.set('authorization', this.loggedInAccessToken.id);
      }
      if (data) {
        var payload = data;
        if (typeof data === 'function')
          payload = data.call(this);
        this.http.send(payload);
      }
      this.req = this.http.req;
      var test = this;
      this.http.end(function(err) {
        test.req = test.http.req;
        test.res = test.http.res;
        delete test.url;
        cb();
      });
    });

    cb();
  });
}

I fixed the problem locally by changing the last part of the function to:

      if (data && typeof data === 'function') {
        this.payload = data.call(this);
      } else {
        this.payload = this.args;
      }
      this.http.send(this.payload);
      this.req = this.http.req;
      var test = this;
      this.http.end(function(err) {
        test.req = test.http.req;
        test.res = test.http.res;
        delete test.url;
        delete test.payload;
        cb();
      });

I just started looking at this library yesterday so I could be doing something wrong. I'm not sure at this point. Any help is appreciated, thanks!

zero5100 commented 9 years ago

You should use this method by passing the data to the helper method that you are using.

  lt.describe.whenCalledRemotely("POST", "/api/test", {
    key1: "value1"
  }, function() {

    lt.it.shouldBeAllowed();
  });
vanraizen commented 9 years ago

Good catch, I'm an idiot :)

Thanks!