Last active
August 29, 2015 14:21
-
-
Save afcastano/f4700afaae08536b4b5b to your computer and use it in GitHub Desktop.
Promise testing
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 nock = require('nock'); | |
var assert = require('assert'); | |
var sinon = require('sinon'); | |
var weeklygames = require('../../app/server/weeklygames.js'); | |
var gamesData = require('./weeklygames-data.json'); | |
describe('weeklygames', function(){ | |
var service; | |
var baseUrl = 'http://basketball.example.com'; | |
beforeEach(function(done){ | |
//Set up mock service. | |
service = nock(baseUrl); | |
service.get('/game/1').reply(200, gamesData.game1); | |
service.get('/game/2').reply(200, gamesData.game2); | |
service.get('/game/3').reply(200, gamesData.game3); | |
service.get('/game/4').reply(200, gamesData.game4); | |
service.get('/game/5').reply(200, gamesData.game5); | |
done(); | |
}); | |
describe('#getFormattedResults', function(){ | |
//The function receives a parameter which is another function that indicates when the test has finished. | |
it('should get the list of formatted results (team_1 score_1 - score_2 team_2)', function(done){ | |
service.get('/thisweek.json').reply(200, gamesData.thisWeek); | |
//Calling the method that returns a promise | |
weeklygames.getFormattedResults() | |
.then(function(results){ | |
//Inside the Async promise result | |
try { | |
assert.equal(results.length, 4); | |
assert.equal(results[0], 'Lakers 79 - 99 Bulls'); | |
assert.equal(results[1], 'Heat 88 - 67 Clippers'); | |
assert.equal(results[2], 'Kings 105 - 101 Suns'); | |
assert.equal(results[3], 'Hawks 115 - 99 Hornets'); | |
//After checking everything call done to finish the test. | |
done(); | |
} catch(err) { | |
done(err); | |
} | |
}); | |
}); | |
it('should get the list of formatted results when there is only one result', function(done){ | |
service.get('/thisweek.json').reply(200, gamesData.thisWeek1); | |
weeklygames.getFormattedResults() | |
.then(function(results){ | |
try { | |
assert.equal(results.length, 1); | |
assert.equal(results[0], 'Hawks 115 - 99 Suns'); | |
done(); | |
} catch(err) { | |
done(err); | |
} | |
}); | |
}); | |
it('should get empty list when there are no results for that week.', function(done){ | |
service.get('/thisweek.json').reply(200, gamesData.weekWithNoGames); | |
weeklygames.getFormattedResults() | |
.then(function(results){ | |
try { | |
assert.equal(results.length, 0); | |
done(); | |
} catch(err) { | |
done(err); | |
} | |
}); | |
}); | |
it('should get an error message when the service returns an error', function(done){ | |
service.get('/thisweek.json').replyWithError('Server error!'); | |
weeklygames.getFormattedResults() | |
.then(function(results){ | |
done('then method should not be called'); | |
}) | |
.catch(function(err) { | |
try { | |
assert.equal(err, 'Error: Server error!'); | |
done(); | |
} catch(e) { | |
done(e); | |
} | |
}); | |
}); | |
}); | |
describe('#printWeeklyGames', function(){ | |
var sandbox; | |
beforeEach(function(done){ | |
//stub console.log | |
sandbox = sinon.sandbox.create(); | |
sandbox.stub(console, 'log'); | |
done(); | |
}); | |
it('should print the weekly games report to console', function(done){ | |
service.get('/thisweek.json').reply(200, gamesData.thisWeek); | |
weeklygames.printWeeklyGames().then(function(){ | |
try{ | |
sinon.assert.calledWithExactly(console.log, 'Lakers 79 - 99 Bulls\n' + | |
'Heat 88 - 67 Clippers\n' + | |
'Kings 105 - 101 Suns\n' + | |
'Hawks 115 - 99 Hornets'); | |
done(); | |
} catch(e) { | |
done(e); | |
} | |
}); | |
}); | |
afterEach(function(done){ | |
sandbox.restore(); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment