【问题标题】:Node.js testing RESTful API (vows.js?)Node.js 测试 RESTful API (vows.js?)
【发布时间】:2011-10-30 22:21:00
【问题描述】:

我真的可以根据一些建议来测试我在 node.js 中创建的 RESTful api。那里有太多的框架,我不知所措。我的测试知识通常不够好,这就是我尝试编写这些测试的原因。我已经尝试过看起来不错的 vows.js,但我不知道如何整合我的 API 测试,我需要某种客户端。我只需要一个测试登录系统的简单帖子示例即可。

【问题讨论】:

    标签: testing node.js vows


    【解决方案1】:

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

    基本测试:

    //
    // 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 })
    

    【讨论】:

      【解决方案2】:

      6 个月后更新

      誓言很糟糕。使用mocha

      原创

      更新为vow-is 代码

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

      // 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

      【讨论】:

        【解决方案3】:

        我使用过vowsjsrequest 库。

        我发现它们是最简单的,因为这两个库都已正确记录并且似乎正在积极开发和维护。 (我还没有发现 APIeasy 的文档就足够了。)

        这是我正在写的example test,用于测试 Couchapp 的 HTTP API:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-28
          • 2018-09-13
          • 2016-01-08
          • 2015-07-30
          • 1970-01-01
          • 2021-11-02
          相关资源
          最近更新 更多