Last active
February 11, 2024 22:24
-
-
Save gravesm/5697874 to your computer and use it in GitHub Desktop.
Jasmine+AMD test stub module injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This module allows you to inject test stubs into the module loading process | |
* during jasmine unit testing. | |
*/ | |
define(["underscore"], function(_) { | |
var uid = 0; | |
function defineMock(mockid, mock) { | |
define(mockid, function() { | |
return mock; | |
}); | |
} | |
function Stubit() { | |
var i, ctx, locals, context, mock, mocks, config, | |
loaded = false; | |
config = _.clone(requirejs.s.contexts._.config); | |
uid++; | |
context = { | |
context: "teststub" + uid, | |
map: { | |
"*": {} | |
} | |
}; | |
mocks = Array.prototype.slice.call(arguments); | |
for (i=0; i<mocks.length; i++) { | |
if (_.isArray(mocks[i][1])) { | |
mock = jasmine.createSpyObj.apply(this, mocks[i]); | |
} else { | |
mock = mocks[i][1]; | |
} | |
mockname = mocks[i][0]; | |
defineMock(mockname+uid, mock); | |
context.map["*"][mockname] = mockname+uid; | |
} | |
ctx = requirejs.config(_.extend(config, context)); | |
return function(modules, spec) { | |
runs(function() { | |
ctx(modules, function() { | |
locals = Array.prototype.slice.call(arguments); | |
loaded = true; | |
}); | |
}); | |
waitsFor(function() { | |
return loaded; | |
}); | |
runs(function() { | |
spec.apply(this, locals); | |
}); | |
}; | |
} | |
return Stubit; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment