Skip to content

Instantly share code, notes, and snippets.

@froots
Forked from danscotton/gist:3164353
Created July 23, 2012 15:58
Show Gist options
  • Save froots/3164377 to your computer and use it in GitHub Desktop.
Save froots/3164377 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', '/path/to/dependency'], function(my_object, myDependency) {
beforeEach(function() {
sinon.stub(myDependency, 'doSomethingWithIt').returns('whatever');
});
afterEach(function() {
myDependency.doSomethingWithIt.restore();
});
it('should call one() as we expect', function() {
expect(my_object.one(), 'blah');
});
});
@froots
Copy link
Author

froots commented Jul 23, 2012

You should be able to include the dependency in your test and stub methods on its external API and have those stubbed methods be used in your 'my_object' code.

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