2017-04-11 13 views
2

나는 express와 함께 Swagger Node을 사용하고 있으며, 골격 프로젝트를 초기화했다. Swagger project create hello-worldsupertest를 사용하여 헬퍼 스텁을 스 태거 노드에 바인드하는 방법은 무엇입니까?

내 안에 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(); 
      }); 
     }); 
+0

supertest를 사용하고있는 부분을 공유 할 수 있습니까? –

+0

@TalhaAwan supertest 부분을 추가했습니다. 관심을 가져 주셔서 감사합니다 – Quinma

답변

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); 
}); 
+0

도움 주셔서 감사합니다! – Quinma