Well, maybe I overdo the unit tests.
Right now, I mock every external thing the function calls.
I want it to be completely separated from all other things (be able to run
without db, without internet, without browser)
Alexey, what would be your test for this controller function without db
mocking.
// Code
SettingsController.prototype.sportsUpdate = function (app, user, data, cb) {
// get array of sports from the passed form
// sports{"sport_name" => sport, ...}
var sports = _.values(data.sports)
, userId = user.id
Seq()
.seq(function () {
// remove old user's sports
app.UserSport.destroyAll({where: {user_id: userId}}, this)
})
.set(sports)
.parEach(function (sport) {
// add new sport
app.UserSport.create({
sport_id: sport.sport_id
, user_id: userId
, anaerobic_threshold: sport.anaerobic_threshold
, aerobic_threshold: sport.aerobic_threshold
}, this)
})
.seq(function () {
cb(null)
})
}
// Test
'test sportsUpdate action': function () {
var gently = new (require('gently'))
, settingsController = new SettingsController()
, app = {UserSport: {}}
, user = {id: 2}
, data = {sports: {ski: {
sport_id: 1
, anaerobic_threshold: 180
, aerobic_threshold: 150
}, run: {
sport_id: 2
, anaerobic_threshold: 160
, aerobic_threshold: 120
}}}
gently.expect(app.UserSport, "destroyAll", function (obj, fn) {
obj.where.user_id.should.equal(2)
fn()
})
gently.expect(app.UserSport, "create", function (obj, fn) {
obj.sport_id.should.equal(1)
obj.user_id.should.equal(2)
obj.anaerobic_threshold.should.equal(180)
obj.aerobic_threshold.should.equal(150)
fn()
})
gently.expect(app.UserSport, "create", function (obj, fn) {
obj.sport_id.should.equal(2)
fn()
})
var cb = gently.expect(function (err) {})
settingsController.sportsUpdate(app, user, data, cb)
},
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en