如何使用Supertest / Superagent模拟Strongloop的更新方法?

时间:2015-06-01 19:50:12

标签: node.js mocha loopbackjs strongloop superagent

我正在使用supertest和mocha进行一些针对strongloop / loopback API的测试。其中一个标准端点是Model / update。更新实际上是PersistedModel.updateAll的一种形式,它接受查询,然后发布到与查询匹配的所有条目。这是成功请求在资源管理器中的样子:

从图片中注意到请求URL主要只是一个查询字符串,并且它返回204.我从superagent docs知道你可以用帖子提交查询。但是我在测试中重复这个问题很麻烦。 以下是我的要求声明:

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

以下是我的测试

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();
  });
});

第一个测试通过,这意味着它返回204作为请求的状态,但它未通过第二个测试意味着即使它发现查询可接受,它实际上并未应用更新。我尝试了许多不同的配方,但没有一个有效。请让我知道我怎么可能模拟这个!在此先感谢您的帮助!

0 个答案:

没有答案