Как привязать хелперную заглушку к сваггер-узлу с помощью супертеста?
Я использую Swagger Node с экспрессом и инициализировал скелетный проект. Swagger project create hello-world
Тогда внутри hello-world/api/controllers/hello_world.js
Я добавил небольшую модификацию, чтобы требовать помощника hello_helper.js
и вызвать его функцию helloHelper.getName()
,
'use strict';
let helloHelper = require('../helpers/hello_helper');
var util = require('util');
module.exports = {
hello: hello
};
function hello(req, res) {
var name = req.swagger.params.name.value || helloHelper.getName();
var hello = util.format('Hello, %s!', name);
res.json(hello);
}
привет-мир / API / хелперы /hello_helper.js
'use strict';
module.exports = {getName: getName};
function getName() {
return 'Ted';
}
Я бы хотел заглушить helloHelper.getName()
возвращать 'Bob'
вместо. Я могу сделать это легко с:hello-world/test/api/controllers/hello_world.js
// Create stub import of hello_helper
mockHelloHelper = proxyquire('../../../api/controllers/hello_world', { '../helpers/hello_helper': { getName: function () { return 'Bob'; } }
});
Используя supertest, как я могу заставить чванства распознать мою заглушку?
РЕДАКТИРОВАТЬ: Благодаря помощи из ответа ниже это решение работает для меня.
var app, getNameStub, mockHelloHelper, request;
beforeEach(function (done) {
// Create stub import of hello_helper
mockHelloHelper = proxyquire('../../../api/controllers/hello_world', {
'../helpers/hello_helper': {
getName: function () {
return 'Bob';
}
}
});
app = require('../../../app');
request = supertest(app);
done();
});
...
it('should return a default string', function(done) {
request
.get('/hello')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
should.not.exist(err);
res.body.should.eql('Hello, Bob!');
done();
});
});
1 ответ
Вам нужно инициализировать / требовать экспресс app
после того, как вы прокси приобрели свою зависимость. Только тогда он может использовать вашу заглушенную версию getName
:
beforeEach(function () {
mockHelloHelper = proxyquire('../../../api/controllers/hello_world', {
'../helpers/hello_helper': {
getName: function () {
return 'Bob';
}
}
});
// initialize/require your app here
request = supertest(app);
});