Skip to content

Instantly share code, notes, and snippets.

@keithamus
Created November 22, 2012 12:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithamus/4130885 to your computer and use it in GitHub Desktop.
Save keithamus/4130885 to your computer and use it in GitHub Desktop.
How to do Dependency Injection in RequireJS?
define([], function () {
return function HypotheticalHelperMethod() {
doSomeOtherStuff();
}
});
define(["helperMethod"], function (helperMethod) {
MyClass = function () {
this.init()
}
MyClass.prototype.init = function () {
helperMethod(1, 2, 3);
}
});
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);
});
});
});
@hisapy
Copy link

hisapy commented Nov 19, 2014

@keithamus did you find a way to test that your helperMethod is called?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment