UPDATESupertest маршруты с притворным услуг
Я обновил код, приведенный ниже, чтобы отразить мое решение. Было довольно сложно понять это, но, надеюсь, это тоже поможет кому-то другому.
Я пытаюсь выяснить, как проверить мои маршруты. Проблема, с которой я сталкиваюсь, заключается в том, что когда я делаю запрос GET
, мой сервис node-googleplaces
вызывает google api. Есть ли способ издеваться над этим сервисом, чтобы я мог проверить свой маршрут и просто подделать данные, которые он возвращает?
controller.js
'use strict';
var path = require('path'),
GooglePlaces = require('node-googleplaces');
exports.placesDetails = function (req, res) {
var places = new GooglePlaces('MY_KEY');
var params = {
placeid: req.params.placeId,
};
//this method call will be replaced by the test stub
places.details(params, function (err, response) {
var updatedResponse = 'updated body here'
res.send(updatedResponse)
});
};
test.js
var should = require('should'),
//seem weird but include it. The new version we're making will get injected into the app
GooglePlaces = require('node-googleplaces');
request = require('supertest'),
path = require('path'),
sinon = require('sinon'),
describe(function() {
before(function (done) {
//create your stub here before the "app" gets instantiated. This will ensure that our stubbed version of the library will get used in the controller rather than the "live" version
var createStub = sinon.stub(GooglePlaces, 'details');
//this will call our places.details callback with the 2nd parameter filled in with 'hello world'.
createStub.yields(null, 'hello world');
app = express.init(mongoose);
agent = request.agent(app);
done();
});
it('should get the data', function (done) {
agent.get('/api/gapi/places/search/elmersbbq')
.end(function (err, res) {
if (err) {
return done(err);
}
console.log(res.body)
done();
});
});
})