首頁 > web前端 > js教程 > 主體

使用 Node.js 做 Function Test實作方法_javascript技巧

WBOY
發布: 2016-05-16 17:18:53
原創
1340 人瀏覽過

Info
上週 meeting 上同事說他們現在在用 java 寫 function test,產生了很多冗餘的程式碼,整個專案也變得比較臃腫。現在迫切需要個簡單的模板專案能快速建立function test。

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

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

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

在這裡我們選用Express當server。

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

複製程式碼 代碼如下:

{
" 🎜>

{
"  ",
    "description": "Wine Cellar Application",
    "version": "0.0.1",
    "private": true,🎜>   ": "3.x"
    }

接下來run command

程式碼如下:

npm install

這樣express就裝上了。

我們實作幾個簡單的 get post 方法來做實驗

程式碼如下:


var express = require('express')

var express = require('express')

var express = require('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();
    res.end();
    res.end(); ;
  }, 200);
});

app.get('/hi', function (req, res) {
  if (req.param('hello') !== undefined) {
    res.writeHead(200, {'Contentent -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(

      [      ]
    );
});

app.get('/user/:id', function(req, res) {
    res.send({
        description: "I am node js"    });

});

app.post('/user/edit', function (req, res) {
  setTimeout(function () {
    res.send({
  ),
      status:1
    });
  }, 100);
}); 
app.listen(3000);

}); app.listen(3000);

}); 
app.listen(3000);

testosterone
server 架設完畢,自然要開始做測試了。

這個 project 的介面的命名都挺優雅,直接上碼。 首先是測試基本的功能
複製程式碼

程式碼如下:



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')({ : 3000})
  , assert = testosterone.assert;

testosterone 
  .get('/user', function (res) {
    var expectRes = [
      >    ];

    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.dedion,"Iam);

  })




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

複製程式碼 程式碼如下:var testosterone = require('testosterone')({ : 3000, title: 'test user api'})
  , add = testosterone.add
  , assert = testosterone.assert;

testosterone
  .add(    'GIVEN a user id  to /user/{id}  n'

    'WHEN 

    function (cb) {
      testosterone.get('/user/1', cb(function (res) {
    ,

            name: "node js",

            描述: "I am node js"
        };

        assert.equal(res.statusCode, 200);
        assert.equal(JSON.stringify(JSON.parse(res.body)) JSON.strcom );
  })

  .add(
    'GIVEN a POST  a user info to /user/edit n'
    'WHEN find 遠>
    function (cb) {

      testosterone.post('/user/edit', {data: {id: 1, name: "change name"}}, cb(function (res) {        assert.equal(res.status, 1);
      }));  
  .run(function () {
    require('sys').print('done!');
  });





Conclusion

透過以上的程式碼,可以看出,同java 冗長的 http 頭設定等,testosterone確實簡單和優雅了不少。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!