나는 express와 함께 Swagger Node을 사용하고 있으며, 골격 프로젝트를 초기화했다. Swagger project create hello-world
supertest를 사용하여 헬퍼 스텁을 스 태거 노드에 바인드하는 방법은 무엇입니까?
내 안에 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';
}
대신 'Bob'
을 반환 helloHelper.getName()
를 스텁합니다. 내가 함께 그렇게 쉽게 수행 할 수 있습니다 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();
});
});
supertest를 사용하고있는 부분을 공유 할 수 있습니까? –
@TalhaAwan supertest 부분을 추가했습니다. 관심을 가져 주셔서 감사합니다 – Quinma