Last active
August 29, 2015 14:16
-
-
Save jimCresswell/73f04b253568b5636790 to your computer and use it in GitHub Desktop.
Resolving expectations against an object of promises
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
| /** | |
| * Return a promise for an object of test values | |
| */ | |
| var Q = require('q'); | |
| var promise1 = getPromiseForValue('someArg1'); | |
| var promise2 = getPromiseForValue('someArg2'); | |
| var promise3 = getPromiseForValue('someArg3'); | |
| return Q.all([ | |
| promise1, | |
| promise2, | |
| promise3 | |
| ]) | |
| .then(function(arrayOfResolvedValues) { | |
| return { | |
| // Need to maintain values of the keys, brittle | |
| key1: arrayOfResolvedValues[1], | |
| key2: arrayOfResolvedValues[2], | |
| key3: arrayOfResolvedValues[3], | |
| }; | |
| }); |
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
| /** | |
| * When you need to test assertions against an object of promised values. | |
| */ | |
| // Library for dealing with prommises. | |
| var Q = require('q'); | |
| var callback; // From step definition | |
| var objectOfExpectedValues; // Keys in same order as returned object of promises | |
| var objectOfPromises = getTestResults(); | |
| var keys = Object.keys(objectOfPromises); | |
| var arrayOfPromises = keys.map(function(key) {return objectOfPromises[key];}); | |
| function doTest(resolvedTestValue, index) { | |
| var currentKey = keys[index]; | |
| expect(arrayOfExpectedValues[currentKey]).to.equal(resolvedTestValue); | |
| } | |
| Q.all(arrayOfPromises) | |
| .then(function(arrayOfResolvedValues) { | |
| resolvedValues.forEach(doTest); | |
| }) | |
| .done(callback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment