使用nock模拟第三方API来测试表达API

时间:2016-06-21 04:54:01

标签: node.js mocha nock

我正在尝试模拟从我的API调用的第三部分API,它似乎无法正常工作。

    describe('Smartvehicle API test', function() {
      it('should return vehicle info', function(done) {
       nock("http://some.api.com")
        .post('/test', '{"id":"1234","responseType":"JSON"}')          
        .reply(200, {
          "status": 200,
          "message": "This is a mocked response"
       });
       request(url)
       .get('/vehicles/1234')
       .end(function(err, res) {
         console.log(res.text);
        res.status.should.be.equal(200);
        res.body.vin.should.be.equal("22");          
        done();
     });      
  })
});

从控制器调用 some.api.com

router.get('/vehicles/:id', function(req, res, next) {
  // Create an instance of our factory that makes vehicles
  var vehicle = vehicleFactory.getVehicle(req.params.id);
  var input = vehicle.vehicleInfo.parseInput(req.params.id);
  requestApi(input, vehicle.vehicleInfo.url, function(err, response) {
    if (err) { 
      next(err);
    } else { 
      res.json(vehicle.vehicleInfo.parseOutput(response));   
    } 
  });
});

0 个答案:

没有答案