2014-03-24 2 views
0

views.js이전에 필요한 모듈을 조롱하지 않았습니까?

controllers = require '../../modules/fixture/controllers.js' 
exports.custom = (db) -> 
    (req, res) -> 
    controllers.custom req.body 
     , (result) -> 
     res.json result : result 
     , (error) -> 
     res.json 400, error : error 

test.js

mockery = require "mockery" 

exports.tests = (app, db, config) -> 
    describe '#routes', -> 
    describe '/fixture/custom', -> 
     it 'should return status 400 if no request body was provided', (done) -> 
     request app 
      .post '/fixture/custom' 
      .send 
      body : 
       players : [] 
      .expect 400, done 

    describe '#views', -> 
    describe '#custom', -> 
     controller = 
     custom : (body, success_callback, error_callback) -> 
      console.log "###MOCK CONTROLLER BODY: #{req}" 
      if body 
      success_callback 'result' 
      else 
      error_callback 'error' 
     mockery.enable() 
     mockery.registerAllowable '../../modules/fixture/controllers.js', true 
     mockery.registerMock '../../modules/fixture/controllers.js', controller 

     it 'should return json keyed by result on success callback', (done) -> 
     req = 
      body : true 
     res.json = (response) -> 
      response.result.should.eql 'result' 
      done() 
     custom req, res 

나는 내가 원하는만큼 등록하기 나의 모의 때문입니다 확인 한 오류 TypeError: Cannot read property 'should' of undefined를 얻을.

test.tests(app, db, config)이 호출되기 전에 require(../../modules/fixture/controllers.js)이 발생하고 있으므로 필요 캐시가 이미 생성되어 있다고 생각합니다.

mockery.registerAllowable('../../modules/fixture/controllers.js', true)은 이미 필요한 모듈을 덮어 쓸 수 있습니까? 많은 테스트를 통해 실제 모듈과 모의 모듈을 전환해야합니다. 예를 들어 명시 적 앱을 인스턴스화하기 전에 "처음부터 모든 시간"이 아닌 "정시에"모의 객체를 등록 할 수 있어야합니다. 이것이 가능한가? 나는 과거에 이것을 사용했습니다

답변

8

내가 조롱을 활성화하기 전에로드 한 모듈을 제거합니다 :

이 옵션을
mockery.enable({ useCleanCache: true }); 

cache of modules is flushed 조롱을 활성화하기 전에.

+0

나는 Yeoman angular-fullstack 프로젝트의 문제점에 직면하고있었습니다. 테스트는 Mocha만으로는 잘 돌아 갔지만 'grunt test'에서는 실패했습니다. Mockery로 만든 모의는 테스트에로드되지 않았습니다. 이것은 분명히 다른 테스트/설정 때문이었습니다. 이전에 테스트 한 모듈이 필요했기 때문에 캐싱되었습니다. 'useCleanCache : true'를 사용하면 나에게 트릭을 보냈다. – Raipe

관련 문제