Created
August 15, 2011 00:53
-
-
Save JulesAU/1145534 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
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