Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created March 20, 2012 13:09
Show Gist options
  • Save unscriptable/2135294 to your computer and use it in GitHub Desktop.
Save unscriptable/2135294 to your computer and use it in GitHub Desktop.
// this is a test module
define(['curl/tdd/createContext', 'require'], function (createContext, require) {
// supply a require (optional) if your module ids are relative
var context = createContext(require);
context.setup(function (require, define /*, complete*/) {
// define all of your mocks and stubs
define('mock1', function () {});
// or fetch them from the server
require(['mock2', 'mock3']);
// or fetch other resources needed for testing
require(['supportModule'], function (support) {
define('mock4', function () { return support.foo(42); });
// complete is redundant here, see comments
/*complete();*/
});
}).cleanup(function (undefine /*, complete*/) {
// undefine any modules that may have been loaded via deep
// dependencies
undefine('module-used-by-moduleToTest');
// complete is not necessary here, see comments
/*complete();*/
}).run(function (require, define, complete) {
// first module (or modules) to test
require(['moduleToTest'], function (moduleToTest) {
// insert tests here
describe('something', function () {
it('should do something', function () {
// do the asserts and such here with moduleToTest...
// if this code is async, you must call complete
complete();
});
});
});
}).run(function (require, define, complete) {
// second module (or modules) to test
require(['moduleToTest2'], function (moduleToTest2) {
// insert tests here
describe('something else', function () {
it('should do something else', function () {
// do the asserts and such here with moduleToTest
// if this code is async, you must call complete
complete();
});
});
});
});
});
@briancavalier
Copy link

This seems really cool, and actually seems to line up well with the direction buster is headed. It'd be interesting if the complete() method could hook into buster's done or vice versa. It'd be great to find a way to get rid of complete(), maybe by returning promises, but that may be outside of curl's domain here, and more a function of the testing framework--somehow curl would need to observe the promises, tho.

I like that context.setup and cleanup parallel the usual unit testing "setup" and "teardown" steps.

My only concern real here is that people will balk at using a curl-specific API in their unit tests. For cujojs, it could be a good thing in that it's an attractive feature for curl to support TDD, but on the other hand, I wonder if using a curl-specific API may turn some people away.

Do you think it's worth pointing the buster folks at this?

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