Skip to content

Instantly share code, notes, and snippets.

@danscotton
Created July 23, 2012 15:52
Show Gist options
  • Save danscotton/3164353 to your computer and use it in GitHub Desktop.
Save danscotton/3164353 to your computer and use it in GitHub Desktop.
mocking with amd
// ----------------------------------------
// /path/to/dependency.js
define(function() {
return {
doSomethingWithIt: function() {
// blah
}
};
});
// ----------------------------------------
// my_object.js
define(['/path/to/dependency'], function(myDependency) { // <-- how do I mock this bugger in my test? :)
function one() {
myDependency.doSomethingWithIt();
return 'blah';
}
function two() {
}
// api
return {
one: one,
two: two
};
});
// ----------------------------------------
// my_object_spec.js
// the object under test is my_object, so ideally
// I'd like to mock/stub myDependency
define(['my_object'], function(my_object) {
it('should call one() as we expect', function() {
expect(my_object.one(), 'blah');
});
});
@danscotton
Copy link
Author

So if I wanted to check whether my_object is calling myDependency.doSomethingWithIt() with the correct number of arguments for example...

Maybe I'm going about it the wrong way though...and should pass myDependency to one like one(myDependency)? (also sorry for the naff function/variable names!)

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