Last active
December 16, 2015 19:59
-
-
Save fnobi/5488620 to your computer and use it in GitHub Desktop.
node.jsでこんなのもテストしたい!! という話 ref: http://qiita.com/fnobi/items/14c9f298d88fc2a2e53d
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
| // テストしたいmoduleをrequire | |
| var hoge = require('../hoge'); | |
| // テスト用のライブラリをrequire (mochaの場合は、shouldがあればいいはず) | |
| var should = require('should'); | |
| // あとはテストを書くのみ | |
| describe('hoge', function () { | |
| it('足し算をする', function () { | |
| hoge.add(2, 3).should.equal(5); | |
| }); | |
| it('割り算をする', function () { | |
| hoge.divide(6, 3).should.equal(2); | |
| }); | |
| it('ゼロで割ったらぬるぽ', function () { | |
| // falseやnullを確認するときはこんな感じ | |
| should.not.exist(hoge.divide(5, 0)); | |
| }); | |
| }); | |
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
| // expressのroutesのみrequire | |
| var routes = require('../routes'); | |
| require('should'); | |
| describe('routes', function () { | |
| var req, res; | |
| beforeEach(function () { | |
| // 簡単に、フェイクのreqとresを用意 | |
| req = {}; | |
| res = { | |
| redirect: function () { }, | |
| render : function () { } | |
| }; | |
| }); | |
| describe('index', function () { | |
| it('should display index page with title', function (done) { | |
| // res.render、すなわち表示用に呼ばれる関数をフェイクで作成 | |
| // 事実上のcallback関数 | |
| res.render = function (view, vars) { | |
| // 'index'というviewを使うことを確認 | |
| view.should.equal('index'); | |
| // 変数'title'が'Express'になっていることを確認 | |
| vars.title.should.eql('Express'); | |
| // テスト終了 | |
| done(); | |
| }; | |
| // 実際に、routesのindexを実行 = アクセスしてみる | |
| routes.index(req, res); | |
| }); | |
| }); | |
| }); |
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
| require('lib/test')(['User']); |
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
| process.env.NODE_ENV = 'test'; | |
| var mongoose = require('mongoose'), | |
| config = require('config'), | |
| async = require('async'); | |
| require('should'); | |
| var test = function (initModels) { | |
| before(function (done) { | |
| async.series([function (next) { | |
| mongoose.connect(config.db.url, next); | |
| }, function (next) { | |
| async.forEach(initModels || [], function (model, next) { | |
| mongoose.model(model).remove(next); | |
| }, next); | |
| }], done); | |
| }); | |
| }; | |
| module.exports = test; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment