Skip to content

Instantly share code, notes, and snippets.

@scichelli
Created December 17, 2011 22:00
Show Gist options
  • Save scichelli/1491537 to your computer and use it in GitHub Desktop.
Save scichelli/1491537 to your computer and use it in GitHub Desktop.
Replacing behavior in JS modules (for test doubles)
<html><body><script type="text/javascript">
var myController = function() {
var ui = function() {
var show = function(message) {
alert("original: " + message);
}
return { show : show }
}();
var sayTwoThings = function() {
ui.show(1); ui.show(2);
}
return {
ui : ui,
do : sayTwoThings
}
}();
var fakeUi = {
show : function(message) { alert("mocked: " + message); }
};
myController.do();
//myController.ui = fakeUi; //this didn't replace behavior; why not?
myController.ui.show = fakeUi.show; // this DID replace behavior.
alert("now we are mocking");
myController.do();
</script></body></html>
@scichelli
Copy link
Author

I want to test myController, so I want to fake the part that creates popups, etc. If myController interacts with a ui, I want my tests to replace myController.ui with a fakeUi. I'm struggling to make this work (see commented line).

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