Created
December 17, 2011 22:00
-
-
Save scichelli/1491537 to your computer and use it in GitHub Desktop.
Replacing behavior in JS modules (for test doubles)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).