Skip to content

Instantly share code, notes, and snippets.

@SimonRichardson
Created February 20, 2013 11:49
Show Gist options
  • Save SimonRichardson/4994980 to your computer and use it in GitHub Desktop.
Save SimonRichardson/4994980 to your computer and use it in GitHub Desktop.
// FUCK HEADS https://github.com/caolan/nodeunit#tests-run-in-series
// No ->
var _readFile = fs.readFile;
fs.readFile = function(path, callback){
// its a stub!
};
// test function that uses fs.readFile
// we're done
fs.readFile = _readFile;
// Yes
// Better -> but could be improved, if you create a wrapper for IO.
var FileProvider = {
get: function(path, callback) {
return fs.readFile(path, callback);
}
};
var MockFileProvider = {
get: function() {
return // My stubby!
}
}
var file = FileProvider.get(path, callback);
// or
var file = MockFileProvider.get(path, callback);
@SimonRichardson
Copy link
Author

Essentially they're testing the wrong code. There should be a mocking lib setup to spy or at least mock the fs.readFile. Don't method swizzle it.

Although it's fun to see who is using it :-)

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