发布于 2016-08-25 01:16:16 | 85 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Node.js教程,程序狗速度看过来!

Node.js 服务器端的JavaScript

Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用· Node.js 借助事件驱动, 非阻塞I/O 模型变得轻量和高效, 非常适合 运行在分布式设备 的 数据密集型 的实时应用


这篇文章介绍了Node.js 做 Function Test实现方法,有需要的朋友可以参考一下

Info
上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿。现在迫切需要个简单的模板项目能快速搭建function test。

后来我回去想了想,为什么我们非得用 java 来做 function test 呢?

Node.js 应该是个不错的选择,并且对 json 有着天然的支持,于是回去在 github 上随手一搜,还果真有相关的项目: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就装上了。

我们实现几个简单的 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 架设完毕,自然要开始做测试了。

这个 project 的接口的命名都挺优雅,直接上代码。

首先是测试基本的功能


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 头设置等,testosterone确实简单和优雅了不少。



最新网友评论  共有(0)条评论 发布评论 返回顶部
推荐阅读
最新资讯

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务