Created
March 20, 2012 13:09
-
-
Save unscriptable/2135294 to your computer and use it in GitHub Desktop.
Possible API for TDD module from https://github.com/cujojs/curl/issues/38#issuecomment-4594777
This file contains 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
// 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(); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'sdone
or vice versa. It'd be great to find a way to get rid ofcomplete()
, 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?