Created
April 11, 2013 22:00
-
-
Save thanpolas/5367556 to your computer and use it in GitHub Desktop.
Testing promises with mocha
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
var assert = require('chai').assert; | |
var when = require('when'); | |
var def1 = when.defer(); | |
function getProm() { | |
return def1.promise; | |
} | |
suite('Test a promise', function() { | |
test('an async test with a promise', function(done){ | |
function onResolve(val) { | |
assert.equal('bar', val, 'Value should be "bar"'); | |
done(); | |
} | |
// the trick is the final .otherwise that will catch | |
// the error thrown in the 'onResolve' function | |
getProm().then(onResolve, done).otherwise(done); | |
}); | |
}); | |
def1.resolve('foo'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wouldnt be cool to adopt the given-when-then of BDD into mocha with something like this:
it('test', function(){
return given().when().then()
});
does anybody know what I am talking about?