使用 Node.js 做 Function Test實(shí)現(xiàn)方法
Info
上周 meeting 上同事說(shuō)他們現(xiàn)在在用 java 寫(xiě) function test,產(chǎn)生了很多冗余的代碼,整個(gè)項(xiàng)目也變得比較臃腫。現(xiàn)在迫切需要個(gè)簡(jiǎn)單的模板項(xiàng)目能快速搭建function test。
后來(lái)我回去想了想,為什么我們非得用 java 來(lái)做 function test 呢?
Node.js 應(yīng)該是個(gè)不錯(cuò)的選擇,并且對(duì) json 有著天然的支持,于是回去在 github 上隨手一搜,還果真有相關(guān)的項(xiàng)目:testosterone,于是便有了這篇blog.
Server
要做demo,自然要有相應(yīng)的server來(lái)支撐。
在這里我們選用Express作為server。
首先我們建立一個(gè)server的文件夾,新建package.json。
{
"name": "wine-cellar",
"description": "Wine Cellar Application",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x"
}
}
接下來(lái)run command
npm install
這樣express就裝上了。
我們實(shí)現(xiàn)幾個(gè)簡(jiǎn)單的 get post 方法來(lái)做實(shí)驗(yàn)
var express = require('express')
, app = express();
app.use(express.bodyParser());
app.get('/hello', function(req, res) {
res.send("hello world");
});
app.get('/', function (req, res) {
setTimeout(function () {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
}, 200);
});
app.get('/hi', function (req, res) {
if (req.param('hello') !== undefined) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello!');
} else {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('use post instead');
}
});
app.post('/hi', function (req, res) {
setTimeout(function () {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(req.param('message') || 'message');
}, 100);
});
app.get('/user', function(req, res) {
res.send(
[
{name:'jack'},
{name:'tom'}
]
);
});
app.get('/user/:id', function(req, res) {
res.send({
id: 1,
name: "node js",
description: "I am node js"
});
});
app.post('/user/edit', function (req, res) {
setTimeout(function () {
res.send({
id:req.param('id'),
status:1
});
}, 100);
});
app.listen(3000);
console.log('Listening on port 3000...');
testosterone
server 架設(shè)完畢,自然要開(kāi)始做測(cè)試了。
這個(gè) project 的接口的命名都挺優(yōu)雅,直接上代碼。
首先是測(cè)試基本的功能
var testosterone = require('testosterone')({port: 3000})
, assert = testosterone.assert;
testosterone
.get('/hello',function(res){
assert.equal(res.statusCode, 200);
})
.get('/hi',function(res){
assert.equal(res.statusCode, 500);
})
.post('/hi', {data: {message: 'hola'}}, {
status: 200
,body: 'hola'
});
然后針對(duì)上面模擬的user的get post 做簡(jiǎn)單的測(cè)試。
var testosterone = require('testosterone')({port: 3000})
, assert = testosterone.assert;
testosterone
.get('/user', function (res) {
var expectRes = [
{name:'jack'},
{name:'tom'}
];
assert.equal(res.statusCode, 200);
assert.equal(JSON.stringify(JSON.parse(res.body)),JSON.stringify(expectRes));
})
.get('/user/1', function (res) {
var user = JSON.parse(res.body);
assert.equal(res.statusCode, 200);
assert.equal(user.name, "node js");
assert.equal(user.description,"I am node js");
})
接下來(lái),如果你想要針對(duì)每個(gè)test case 用 give when then 來(lái)描述的話,可以這樣:
var testosterone = require('testosterone')({port: 3000, title: 'test user api'})
, add = testosterone.add
, assert = testosterone.assert;
testosterone
.add(
'GIVEN a user id to /user/{id} \n' +
'WHEN it have response user \n' +
'THEN it should return user json',
function (cb) {
testosterone.get('/user/1', cb(function (res) {
var expectRes = {
id: 1,
name: "node js",
description: "I am node js"
};
assert.equal(res.statusCode, 200);
assert.equal(JSON.stringify(JSON.parse(res.body)), JSON.stringify(expectRes));
}));
})
.add(
'GIVEN a POST a user info to /user/edit \n' +
'WHEN find modify success \n' +
'THEN it should resturn status 1',
function (cb) {
testosterone.post('/user/edit', {data: {id: 1, name: "change name"}}, cb(function (res) {
var res = JSON.parse(res.body);
assert.equal(res.status, 1);
}));
}
)
.run(function () {
require('sys').print('done!');
});
Conclusion
通過(guò)以上的代碼,可以看出,同java 冗長(zhǎng)的 http 頭設(shè)置等,testosterone確實(shí)簡(jiǎn)單和優(yōu)雅了不少。
- node.js中的socket.io入門(mén)實(shí)例
- node.js入門(mén)教程迷你書(shū)、node.js入門(mén)web應(yīng)用開(kāi)發(fā)完全示例
- Node.js模擬瀏覽器文件上傳示例
- Node.js開(kāi)發(fā)指南中的簡(jiǎn)單實(shí)例(mysql版)
- 分析Node.js connect ECONNREFUSED錯(cuò)誤
- Node.js實(shí)戰(zhàn) 建立簡(jiǎn)單的Web服務(wù)器
- Ubuntu 11.10 安裝Node.js的方法
- 在Windows上安裝Node.js模塊的方法
- Node.js:Windows7下搭建的Node.js服務(wù)(來(lái)玩玩服務(wù)器端的javascript吧,這可不是前端js插件)
- 跟我學(xué)Nodejs(一)--- Node.js簡(jiǎn)介及安裝開(kāi)發(fā)環(huán)境
相關(guān)文章
javascript 7行代碼畫(huà)出一個(gè)圍棋棋盤(pán)
javascript 只有7行代碼即可畫(huà)出圍棋棋盤(pán)的實(shí)現(xiàn)代碼。大家可以看看。2009-07-07淺析JavaScript回調(diào)函數(shù)應(yīng)用
這篇文章主要為大家詳細(xì)介紹了JavaScript回調(diào)函數(shù)應(yīng)用,感興趣的朋友可以參考一下2016-05-05微信小程序?qū)W習(xí)總結(jié)(三)條件、模板、文件引用實(shí)例分析
這篇文章主要介紹了微信小程序條件、模板、文件引用,結(jié)合實(shí)例形式分析了微信小程序if條件判斷、模板調(diào)用、wxss文件引用等相關(guān)操作技巧,需要的朋友可以參考下2020-06-06IE11下使用canvas.toDataURL報(bào)SecurityError錯(cuò)誤的解決方法
這篇文章主要給大家介紹了關(guān)于在IE11下使用canvas.toDataURL報(bào)SecurityError錯(cuò)誤的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)同樣遇到這個(gè)問(wèn)題的朋友們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11JavaScript中防止微信瀏覽器被整體拖動(dòng)的方法
這篇文章主要介紹了JavaScript中防止微信瀏覽器被整體拖動(dòng)的方法,需要的朋友可以參考下2017-08-08