Skip to content

Instantly share code, notes, and snippets.

@commuterjoy
Created June 21, 2012 15:48
Show Gist options
  • Save commuterjoy/2966561 to your computer and use it in GitHub Desktop.
Save commuterjoy/2966561 to your computer and use it in GitHub Desktop.
Mocking AMD modules
// Idea that you can use the 'paths' property to swap in a mock AMD module
// during testing.
-- test/runner.html
23 var guardian = {
24 page: {
25 omnitureAccount: "guardiangu-fake"
26 },
27 js: {
28 modules: {
29 bean: 'vendor/bean-0.4.11-1/bean',
30 domReady: 'vendor/require-2.0.2/plugins/domReady',
31 detect: 'mocks/detect' // <-------- mock path
32 }
33 }
34 };
35
36 requirejs.config({
37 paths: guardian.js.modules
38 });
-- ./mocks/detect.js
2 define([], function(){
3 return {
4 speed: 'slow',
5 getLayoutMode: function() { return 1; },
6 getConnectionSpeed: function(){ return this.speed; }
7 }
8 });
-- ./src/foo.js <-- production code using the 'detect' module
1 define(['detect'], function() {
2 return { foo = function(){ detect.getConnectionSpeed(); } }
3 }
-- ./spec/foo.js <-- test code includes the production src module
1 define(['foo'], function() {
2 it("should ...", function(){
3 // test
4 });
5 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment