首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js testing RESTful API (vows.js?)

Node.js testing RESTful API (vows.js?)
EN

Stack Overflow用户
提问于 2011-08-20 04:47:04
回答 3查看 6.1K关注 0票数 8

我真的需要一些关于测试我在node.js中创建的RESTful应用程序接口的建议。外面有太多的框架,我不知所措。一般来说,我的测试知识还不够好,这就是为什么我要尝试写这些测试。我已经尝试过vows.js,它看起来不错,但是我不知道如何整合我的应用程序接口的测试,我需要一些客户端。测试登录系统的一个简单的post示例就是我所需要的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-20 07:33:23

6个月后更新

誓言-这太烂了。使用mocha

原创

已使用 代码进行更新

这是来自vows-is examples folder的vows-is示例。

代码语言:javascript
复制
// simple HTTP
// Run with node example/simple-http.js

var express = require("express");
    is = require("../src/vows-is.js");

is.config({
    "server": {
        "factory": function _factory(cb) {
            var app = express.createServer();

            app.get("/", function(req, res) {
                res.send("hello world");
            })

            app.listen(3000);

            cb(app);
        },
        "uri": "http://localhost:3000",
        "kill": function _kill(app) {
            app.close();
        }
    }
});

is.suite("http request test").batch()

    .context("a request to GET /")
        .topic.is.a.request("GET /")
        .vow.it.should.have.status(200)
        .vow.it.should.have
            .header("content-type", "text/html; charset=utf-8")
        .context("contains a body that")
            .topic.is.property('body')
            .vow.it.should.be.ok
            .vow.it.should.include.string("hello world")

.suite().run({
    reporter: is.reporter
}, function() {
    console.log("finished");
    is.end();
})

这使用vows-is

票数 7
EN

Stack Overflow用户

发布于 2011-09-01 01:04:21

http://blog.nodejitsu.com/rest-easy-test-any-api-in-nodejs正是为此目的而设计的。它是一个位于誓言之上的DSL,它简化了使用誓言写出测试的过程。

基本测试:

代码语言:javascript
复制
//
// Here we will configure our tests to use 
// http://localhost:8080 as the remote address
// and to always send 'Content-Type': 'application/json'
//
suite.use('localhost', 8000)
     .setHeader('Content-Type', 'application/json');
     //
     // A GET Request to /ping
     //   should respond with 200
     //   should respond with { pong: true }
     //
     .get('/ping')
       .expect(200, { pong: true })
     //
     // A POST Request to /ping
     //   should respond with 200
     //   should respond with { dynamic_data: true }
     //
     .post('/ping', { dynamic_data: true })
       .expect(200, { dynamic_data: true })
票数 4
EN

Stack Overflow用户

发布于 2011-10-25 23:34:32

我使用过vowsjsrequest库。

我发现它们是最简单的,因为这两个库都有适当的文档,而且似乎都是积极开发和维护的。(我还没有找到足够的APIeasy文档。)

下面是我目前正在编写的一个example test,用于测试Couchapp的HTTP API:

代码语言:javascript
复制
var request = require ('request')
    , vows = require ('vows')
    , assert = require ('assert');

var BASE_URL = "http://local.todos.com:5984/todos/"
    , HEADERS = {
      'Content-Type': 'application/json'
    }
    , revisionReference;

vows.describe ('CouchApp Todos REST API')
  // --------------------------------------------
  // Testing PUTs
  // ============================================
  .addBatch ({
    "A PUT to /todos/test-host without data": {
      topic : function () {
        request ({
          uri: BASE_URL + "test-host",
          method: 'PUT',
          headers: HEADERS
        }, this.callback );
      } 
      , "should respond with 201" : function ( err, res, body ) {
        assert.equal ( res.statusCode, 201 );
      }
      , "should have an id 'test-host'" : function ( err, res, body ) {
        assert.equal ( JSON.parse( res.body )._id, 'test-host' );
      }
      , "response should contain empty todos []" : function ( err, res, body ) {
        assert.include ( JSON.parse( res.body ), 'todos' );
        assert.deepEqual ( JSON.parse( res.body ).todos, [] );
      }
    }
  })
  .addBatch ({
    "A PUT to /todos/test-host with one todo item (an object)" : {
        topic : function () {
          request ({
            uri: BASE_URL + "test-host"
            , body: JSON.stringify({
                "title" : "Testing Todo",
                "isDone" : false
              })
            , method : "PUT"
            , headers : HEADERS
          }, this.callback );
        }
        , "should respond with 201" : function ( err, res, body ) {
          assert.equal ( res.statusCode, 201 );
        }
        , "should have an id 'test-host'" : function ( err, res, body ) {
          assert.equal ( JSON.parse( res.body )._id, 'test-host' )
        }
        , "response should contain todos array with one item" : function ( err, res, body ) {
          assert.include ( JSON.parse( res.body ), 'todos' );
          assert.deepEqual ( 
            JSON.parse( res.body ).todos
            , [{
              "title" : "Testing Todo",
              "isDone" : false,
              "_id" : 0
            }] 
          );
        }
    }
  })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7127226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档