From 50e15fe5782854e5e69c2bcdd1c48db8a5301dad Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 13 Jun 2012 21:26:28 +0800 Subject: [PATCH] add rewire for private methods test. --- .jshintrc | 1 + Makefile | 9 +++------ controllers/at.js | 5 ++--- package.json | 4 +++- test/controllers/at.js | 33 +++++++++++++++++---------------- test/support/http.js | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.jshintrc b/.jshintrc index 698262e..81fbe5f 100644 --- a/.jshintrc +++ b/.jshintrc @@ -11,6 +11,7 @@ "before", "after", "should", + "rewire", "$" ], diff --git a/Makefile b/Makefile index 1ea54fb..5b1c160 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,10 @@ SRC := libs controllers plugins models TESTS = $(shell find test -type f -name "*.js") TESTTIMEOUT = 5000 REPORTER = spec +JSCOVERAGE = ./node_modules/visionmedia-jscoverage/jscoverage test: + @npm install @if ! test -f config.js; then \ cp config.default.js config.js; \ fi @@ -16,7 +18,7 @@ test-dot: cov: @for dir in $(SRC); do \ mv $$dir $$dir.bak; \ - jscoverage --encoding=utf-8 $$dir.bak $$dir; \ + $(JSCOVERAGE) --encoding=utf-8 $$dir.bak $$dir; \ done cov-clean: @@ -29,9 +31,4 @@ test-cov: cov @-$(MAKE) test REPORTER=html-cov > coverage.html @$(MAKE) cov-clean -test-for: - @for dir in $(SRC); do \ - echo $$dir; \ - done - .PHONY: test test-cov test-dot cov cov-clean diff --git a/controllers/at.js b/controllers/at.js index 093c522..9b253e4 100644 --- a/controllers/at.js +++ b/controllers/at.js @@ -34,7 +34,7 @@ function searchUsers(text, callback) { } function sendMessageToMentionUsers(text, topicId, authorId, callback) { - exports.searchUsers(text, function (err, users) { + searchUsers(text, function (err, users) { if (err || !users || users.length === 0) { return callback && callback(err); } @@ -58,7 +58,7 @@ function sendMessageToMentionUsers(text, topicId, authorId, callback) { } function linkUsers(text, callback) { - exports.searchUsers(text, function (err, users) { + searchUsers(text, function (err, users) { if (err) { return callback(err); } @@ -70,6 +70,5 @@ function linkUsers(text, callback) { }); } -exports.searchUsers = searchUsers; exports.send_at_message = exports.sendMessageToMentionUsers = sendMessageToMentionUsers; exports.link_at_who = exports.linkUsers = linkUsers; diff --git a/package.json b/package.json index 9d3a5b7..043bc3b 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ }, "devDependencies": { "should": ">=0.6.0", - "mocha": ">=0.14.1" + "mocha": ">=0.14.1", + "rewire": ">=0.2.0", + "visionmedia-jscoverage": ">=1.0.0" }, "scripts": { "test": "make test" diff --git a/test/controllers/at.js b/test/controllers/at.js index 550142a..fe11e84 100644 --- a/test/controllers/at.js +++ b/test/controllers/at.js @@ -7,9 +7,8 @@ /** * Module dependencies. */ - +var rewire = require("rewire"); var should = require('should'); -var mentionUser = require('../../controllers/at'); var Message = require('../../controllers/message'); var config = require('../../config').config; var createUsers = require('../support/create_test_users').createUsers; @@ -30,9 +29,10 @@ describe('controllers/at.js', function () { @testuser2@testuser123 oh my god'; describe('searchUsers()', function () { - + var mentionUser = rewire('../../controllers/at'); + var searchUsers = mentionUser.__get__('searchUsers'); it('should found 3 test users', function (done) { - mentionUser.searchUsers(text, function (err, users) { + searchUsers(text, function (err, users) { should.not.exist(err); should.exist(users); users.should.length(3); @@ -45,7 +45,7 @@ describe('controllers/at.js', function () { }); it('should found 0 user in text', function (done) { - mentionUser.searchUsers('no users match in text @ @@@@ @ @@@ @哈哈 @ testuser1', function (err, users) { + searchUsers('no users match in text @ @@@@ @ @@@ @哈哈 @ testuser1', function (err, users) { should.not.exist(err); should.exist(users); users.should.length(0); @@ -54,7 +54,7 @@ describe('controllers/at.js', function () { }); it('should found 0 user in db', function (done) { - mentionUser.searchUsers('@testuser123 @suqian2012 @ testuser1 no users match in db @ @@@@ @ @@@', + searchUsers('@testuser123 @suqian2012 @ testuser1 no users match in db @ @@@@ @ @@@', function (err, users) { should.not.exist(err); should.exist(users); @@ -65,6 +65,7 @@ describe('controllers/at.js', function () { }); describe('linkUsers()', function () { + var mentionUser = rewire('../../controllers/at'); it('should link all mention users', function (done) { mentionUser.linkUsers(text, function (err, text2) { should.not.exist(err); @@ -74,18 +75,17 @@ describe('controllers/at.js', function () { }); describe('mock searchUsers() error', function () { - var searchUsers = mentionUser.searchUsers; before(function () { - mentionUser.searchUsers = function () { - var callback = arguments[arguments.length - 1]; - process.nextTick(function () { - callback(new Error('mock searchUsers() error')); - }); - }; - }); - after(function () { - mentionUser.searchUsers = searchUsers; + mentionUser.__set__({ + searchUsers: function () { + var callback = arguments[arguments.length - 1]; + process.nextTick(function () { + callback(new Error('mock searchUsers() error')); + }); + } + }); }); + it('should return error', function (done) { mentionUser.linkUsers(text, function (err, text2) { should.exist(err); @@ -98,6 +98,7 @@ describe('controllers/at.js', function () { }); describe('sendMessageToMentionUsers()', function () { + var mentionUser = rewire('../../controllers/at'); it('should send message to all mention users', function (done) { mentionUser.sendMessageToMentionUsers(text, '4fb9db9c1dc2160000000005', '4fcae41e1eb86c0000000003', function (err) { diff --git a/test/support/http.js b/test/support/http.js index 212f9c9..bbd3825 100644 --- a/test/support/http.js +++ b/test/support/http.js @@ -23,7 +23,7 @@ express.HTTPServer.prototype.request = function (address) { return new Request(this, address); }; -if (connect) { +if (connect && connect.HTTPServer) { connect.HTTPServer.prototype.request = express.HTTPServer.prototype.request; }