Skip to content

Instantly share code, notes, and snippets.

@JulesAU
Created August 15, 2011 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JulesAU/1145534 to your computer and use it in GitHub Desktop.
Save JulesAU/1145534 to your computer and use it in GitHub Desktop.
/*
We have two classes: resultRenderer and dbQuery
resultRenderer will ask dbQuery to perform a database query, and asyncronously receives the results
*/
function resultRenderer(query) { this._query = query }
resultRenderer.prototype = {
queryResultsAndRender: function () {
this._query.runQueryAsync(this.renderResults.bind(this));
},
renderResults: function (results) {
// now render them!
}
}
function dbQuery() {}
dbQuery.prototype = {
runQueryAsync: function (callback) {
// query the database, and callback once we have results:
callback(queryResults);
}
}
// to test resultRenderer using a mocked dbQuery, we write something like:
query = new dbQuery;
spyOn(dbQuery, 'runQueryAsync');
renderer = new resultRenderer(query);
renderer.queryResultsAndRender();
// expect that our async query method was called, and supplied with an appropriate callback
expect(dbQuery.runQueryAsync).toHaveBeenCalledWith(/* What goes here? */);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment