欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用 Node.js 做 Function Test實現(xiàn)方法

 更新時間:2013年10月25日 15:34:12   作者:  
這篇文章介紹了Node.js 做 Function Test實現(xiàn)方法,有需要的朋友可以參考一下

Info
上周 meeting 上同事說他們現(xiàn)在在用 java 寫 function test,產(chǎn)生了很多冗余的代碼,整個項目也變得比較臃腫。現(xiàn)在迫切需要個簡單的模板項目能快速搭建function test。

后來我回去想了想,為什么我們非得用 java 來做 function test 呢?

Node.js 應該是個不錯的選擇,并且對 json 有著天然的支持,于是回去在 github 上隨手一搜,還果真有相關(guān)的項目:testosterone,于是便有了這篇blog.

Server
要做demo,自然要有相應的server來支撐。

在這里我們選用Express作為server。

首先我們建立一個server的文件夾,新建package.json。

復制代碼 代碼如下:

{
    "name": "wine-cellar",
    "description": "Wine Cellar Application",
    "version": "0.0.1",
    "private": true,
    "dependencies": {
        "express": "3.x"
    }
}
 

接下來run command

復制代碼 代碼如下:

npm install

這樣express就裝上了。

我們實現(xiàn)幾個簡單的 get post 方法來做實驗

復制代碼 代碼如下:

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è)完畢,自然要開始做測試了。

這個 project 的接口的命名都挺優(yōu)雅,直接上代碼。

首先是測試基本的功能

復制代碼 代碼如下:

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'
  });
 

然后針對上面模擬的user的get post 做簡單的測試。

復制代碼 代碼如下:

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");
  })

接下來,如果你想要針對每個test case 用 give when then 來描述的話,可以這樣:

復制代碼 代碼如下:

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
通過以上的代碼,可以看出,同java 冗長的 http 頭設(shè)置等,testosterone確實簡單和優(yōu)雅了不少。

相關(guān)文章

  • JavaScript繼承定義與用法實踐分析

    JavaScript繼承定義與用法實踐分析

    這篇文章主要介紹了JavaScript繼承定義與用法,結(jié)合實例形式分析了JavaScript面向?qū)ο蟪绦蛟O(shè)計中基類的定義、原型繼承以及調(diào)用父類構(gòu)造函數(shù)等相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • javascript 7行代碼畫出一個圍棋棋盤

    javascript 7行代碼畫出一個圍棋棋盤

    javascript 只有7行代碼即可畫出圍棋棋盤的實現(xiàn)代碼。大家可以看看。
    2009-07-07
  • JS取得絕對路徑的實現(xiàn)代碼

    JS取得絕對路徑的實現(xiàn)代碼

    這篇文章主要介紹了JS取得絕對路徑的實現(xiàn)代碼,需要的朋友可以參考下
    2015-01-01
  • 淺析JavaScript回調(diào)函數(shù)應用

    淺析JavaScript回調(diào)函數(shù)應用

    這篇文章主要為大家詳細介紹了JavaScript回調(diào)函數(shù)應用,感興趣的朋友可以參考一下
    2016-05-05
  • 微信小程序組件通信和behavior使用詳解

    微信小程序組件通信和behavior使用詳解

    behaviors是小程序中,用于實現(xiàn)組件間代碼共享的特性,類似于 Vue.js 中的 “mixins”,這篇文章主要介紹了微信小程序組件通信和behavior使用,需要的朋友可以參考下
    2022-08-08
  • 微信小程序?qū)W習總結(jié)(三)條件、模板、文件引用實例分析

    微信小程序?qū)W習總結(jié)(三)條件、模板、文件引用實例分析

    這篇文章主要介紹了微信小程序條件、模板、文件引用,結(jié)合實例形式分析了微信小程序if條件判斷、模板調(diào)用、wxss文件引用等相關(guān)操作技巧,需要的朋友可以參考下
    2020-06-06
  • IE11下使用canvas.toDataURL報SecurityError錯誤的解決方法

    IE11下使用canvas.toDataURL報SecurityError錯誤的解決方法

    這篇文章主要給大家介紹了關(guān)于在IE11下使用canvas.toDataURL報SecurityError錯誤的解決方法,文中通過示例代碼介紹的非常詳細,對同樣遇到這個問題的朋友們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-11-11
  • JavaScript數(shù)組去重的多種方法(四種)

    JavaScript數(shù)組去重的多種方法(四種)

    數(shù)組去重,一般需求是給你一個數(shù)組,調(diào)用去重方法,返回數(shù)值副本,副本中沒有重復元素。下面腳本之家小編給大家?guī)砹薺s數(shù)組去重的多種方法,非常不錯,需要的朋友參考下
    2017-09-09
  • 深入淺析JS中的嚴格模式

    深入淺析JS中的嚴格模式

    嚴格模式就是使JS編碼更加規(guī)范化的模式,消除Javascript語法的一些不合理、不嚴謹之處,減少一些怪異行為。下面通過代碼相結(jié)合的形式給大家介紹js中的嚴格模式,感興趣的朋友一起看看吧
    2018-06-06
  • JavaScript中防止微信瀏覽器被整體拖動的方法

    JavaScript中防止微信瀏覽器被整體拖動的方法

    這篇文章主要介紹了JavaScript中防止微信瀏覽器被整體拖動的方法,需要的朋友可以參考下
    2017-08-08

最新評論