Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Created February 23, 2021 02:05
Show Gist options
  • Save brainysmurf/ff3b2c5e4284cb6730c77c91aff7c269 to your computer and use it in GitHub Desktop.
Save brainysmurf/ff3b2c5e4284cb6730c77c91aff7c269 to your computer and use it in GitHub Desktop.
Illustrating how one can orchestrate mocks using dependency injection
function work_ (url, {UrlFetchApp_=UrlFetchApp}={}) {
Logger.log(url);
return UrlFetchApp_.fetch(url).getContentText();
}
function testWork() {
const url = 'https://example.com';
const response1 = work_(url);
Logger.log(response1);
class Mocked {
static fetch() {
return {
getContentText: () => 'ha'
};
}
}
const response2 = work_(url, {UrlFetchApp_: Mocked});
Logger.log(response2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment