1

I'm working on some tests for strongloop/loopback APIs using supertest and mocha. One of the standard endpoints is Model/update. Update is actually a form of PersistedModel.updateAll which takes in a query and then posts to all entries that match the query. This is a picture of what a successful request looks like in the explorer:

Notice from the picture that the request URL is mainly just a query string, and that it returns 204. I know from the superagent docs that you can submit querys with a post. However I'm having a lot of trouble duplicating this with my tests. Here are my require statements:

var request = require('supertest');
var app = require('../server');
var assert = require('chai').assert;
var chance = require('chance').Chance();

Here are my tests

describe('/api/Points/update', function(){
    var updatedZip = "60000";

it('should grab a Point for a before reference', function(done) {
   json('get', '/api/Points/' +addID )
   .end(function(err, res) {
       assert.equal(res.body.zipcode, addZip, 'unexpected value for zip');   
      done();
    });
  });
it('should update the Point w/ a new zipcode', function(done) {
   var where = {"zipcode": "60035"}; 
   var data ={"zipcode": updatedZip};

   request(app)
    .post('/api/Points/update')
    .query({"where": {"zipcode": "60035"}})
    .send({                 
       data : data
     })
   .end(function(err, res) {
       assert.equal(res.status, 204, 'update didnt take');
       done();
      });
    });
it('should check to see that the Point was updated', function(done) {
  json('get', '/api/Points/' +addID )
  .end(function(err, res) {
      assert.equal(res.body.zipcode, updatedZip, 'updated zip was not applied');    
   done();
  });
});

The first test passes, meaning that it returned a 204 as the status of the request however it fails the second test meaning that even though it found the query acceptable it didn't actually apply the update. I've tried a number of different formulations but none of them have worked. Please let me know how I could possibly simulate this! Thanks in advance for your help!

Ben Zifkin
  • 892
  • 2
  • 8
  • 16
  • Just a guess, it may be because the data as the value of `.query({where:})` needs to be stringified/escaped and it may not be something supertest will do for you? So try: `.query({"where":"%7B%22zipcode%22%3A%20%2260035%22%7D%0A"})` maybe... – pulkitsinghal Jun 02 '15 at 15:15

0 Answers0