Last active
December 18, 2015 13:09
-
-
Save ms2sato/5787373 to your computer and use it in GitHub Desktop.
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
//npm install underscore should | |
var _ = require('underscore'), | |
should = require('should'); | |
/** | |
* 今回のサンプルのサービス | |
* @constructor | |
*/ | |
function Service(){}; | |
_.extend(Service.prototype, { | |
/** | |
* ログインします | |
* @param usery ユーザ | |
* @param pass パス | |
* @param callback コールバック関数 | |
*/ | |
login: function(user, pass, callback){ | |
if(!user){ | |
throw new Error('user should exist'); | |
} | |
if(!pass){ | |
throw new Error('pass should exist'); | |
} | |
if(!callback){ | |
throw new Error('callback should exist'); | |
} | |
// 1秒後処理が終わるとする | |
setTimeout(function(){ | |
if(pass == 'success'){ | |
callback(null, { | |
name: 'Sato', | |
status: 'OK' | |
}); | |
}else{ | |
callback({ | |
status: 'NG', | |
message: 'failed' | |
}); | |
} | |
}, 1000); | |
} | |
}); | |
var service = new Service(); | |
describe('sample test', function () { | |
/** | |
* テスト前処理 | |
*/ | |
before(function (done) { | |
console.log('before'); | |
done(); | |
}); | |
/** | |
* テスト後処理 | |
*/ | |
after(function (done) { | |
console.log('after'); | |
done(); | |
}); | |
/** | |
* ログインのテストをやります | |
*/ | |
describe('login', function () { | |
/** | |
* ログイン成功ケース | |
*/ | |
it('should login success', function (done) { | |
var user = 'Sato'; | |
var pass = 'success'; | |
// Satoは登録ユーザなので成功します | |
service.login(user, pass, function(err, ret){ | |
should.exist(ret); | |
should.not.exist(err); | |
//こういうのもありそう? | |
// if(err){ | |
// done(err); //エラーが返ってくるはずは無いので失敗として返す | |
// } | |
ret.should.have.property('status', 'OK'); //statusプロパティは存在して、'OK' | |
ret.should.have.property('name', 'Sato'); | |
ret.should.not.have.property('message'); //messageプロパティは無い | |
done(); | |
}); | |
}); | |
/** | |
* ログイン失敗ケース | |
*/ | |
it('should login fail', function (done) { | |
var user = 'Suzuki'; | |
var pass = 'evil'; | |
// Suzukiは登録ユーザではないので失敗します | |
service.login(user, pass, function(err, ret){ | |
should.exist(err); | |
should.not.exist(ret); | |
err.should.have.property('status', 'NG'); | |
err.should.have.property('message', 'failed'); | |
done(); | |
}); | |
}); | |
/** | |
* コールバックする前のエラー | |
*/ | |
it('should argument error', function () { | |
try{ | |
// 引数が無いので例外おきます | |
service.login(null, null, function(err, ret){ | |
should.fail('ここには来ません'); | |
}); | |
should.fail('ここには来ません'); | |
}catch(err){ | |
//ここに来ます | |
should.exist(err); | |
} | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment