Skip to content

Instantly share code, notes, and snippets.

@stevehorn
Created October 22, 2010 01:40
Show Gist options
  • Save stevehorn/639760 to your computer and use it in GitHub Desktop.
Save stevehorn/639760 to your computer and use it in GitHub Desktop.
How do I stub and/or redefine a javascript object dependency?
//Code under test
function Foo() {
this.do_something_interesting = function() {
var dependency = new CanYouMockMe();
if(dependency.i_want_stubbed() === true) {
//do stuff based on condition
} else {
//do stuff if false
}
}
}
//Test Code
describe("Foo", function () {
it("should do something if the dependency returns true", function () {
var foo = new Foo();
//how do I stub and/or redefine the "i_want_stubbed" method here?
var result_if_true = foo.do_something_interesting();
expect(result).toEqual(result_if_true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment