node.js - Mockery not mocking previously required modules? -


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', ->       'should return status 400 if no request body 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        'should return json keyed result on success callback', (done) ->         req =           body : true         res.json = (response) ->           response.result.should.eql 'result'           done()         custom req, res 

i error typeerror: cannot read property 'should' of undefined have confirmed due mock not getting registered i'd like.

i'm thinking problem require(../../modules/fixture/controllers.js) happening before test.tests(app, db, config) called, , require cache created.

shouldn't mockery.registerallowable('../../modules/fixture/controllers.js', true) allow me override required modules? require quite lot of switching between real , mock modules throughout many tests, i'd prefer able register mocks "just in time" rather @ beginning, instance before instantiating express app. possible?

i've used in past rid of modules had loaded before enable mockery:

mockery.enable({ usecleancache: true }); 

with option, cache of modules flushed before enabling mockery.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -