Created
November 22, 2012 12:20
-
-
Save keithamus/4130885 to your computer and use it in GitHub Desktop.
How to do Dependency Injection in RequireJS?
This file contains hidden or 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
define([], function () { | |
return function HypotheticalHelperMethod() { | |
doSomeOtherStuff(); | |
} | |
}); | |
define(["helperMethod"], function (helperMethod) { | |
MyClass = function () { | |
this.init() | |
} | |
MyClass.prototype.init = function () { | |
helperMethod(1, 2, 3); | |
} | |
}); |
This file contains hidden or 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
require(["helperMethod", "myClass"], function (helperMethod, myClass) { | |
describe("MyClass tests", function () { | |
it("calls `helperMethod` upon init", function () { | |
// here i need to spyOn helperMethod, but if I set helperMethod | |
// to a jasmime spy: | |
helperMethod = jasmine.createSpy("helperMethod"); | |
// I've altered my reference, and MyClass still has the original | |
// HypotheticalHelperMethod function. | |
var instance = new MyClass(); | |
expect(helperMethod).toHaveBeenCalledWith(1, 2, 3); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@keithamus did you find a way to test that your helperMethod is called?