Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created September 21, 2010 17:11
Show Gist options
  • Save karlseguin/590059 to your computer and use it in GitHub Desktop.
Save karlseguin/590059 to your computer and use it in GitHub Desktop.
function setup(method)
{
method = fake;
};
function fake()
{
return "faked";
}
function real()
{
return "real";
}
setup(real);
console.log(real() == "faked");
/* this works but isn't as pretty */
function setup(method)
{
return fake;
};
...
real = setup(real);
console.log(real() == "faked");
@darrenkopp
Copy link

var test = {
   real: function() { return "real"; }
};

function mock(object, name, mocked) {
    object[name] = mocked;
}

console.log(test.real() == "faked");
mock(test, "real", function() { return "faked"; });
console.log(test.real() == "faked");

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