Skip to content

Instantly share code, notes, and snippets.

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

Hey @froots, just ran into a similar problem again this morning and I have a more concrete example that may explain things a little better of what we're trying to achieve: https://gist.github.com/3187758

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