【问题标题】:Node.js: How to test a functionNode.js:如何测试函数
【发布时间】:2011-09-05 09:11:48
【问题描述】:

如何测试这样的函数?

app.post '/incoming', (req,res) ->
    console.log "Hello, incoming call!"
    message = req.body.Body
    from = req.body.From

    sys.log "From: " + from + ", Message: " + message
    twiml = '<?xml version="1.0" encoding="UTF-8" ?>\n<Response>\n<Say>Thanks for your text, we\'ll be in touch.</Say>\n</Response>'
    res.send twiml, {'Content-Type':'text/xml'}, 200

我还没有选择任何测试框架。我不明白如何测试。

谢谢!

【问题讨论】:

    标签: javascript node.js coffeescript


    【解决方案1】:

    我更喜欢nodeunit 的轻量级语法,结合request 发出HTTP 请求。您将创建一个类似于

    test/test.coffee 文件
    request = require 'request'
    
    exports['Testing /incoming'] = (test) ->
      request 'http://localhost:3000/incoming', (err, res, body) ->
        test.ok !err
        test.equals res.headers['content-type'], 'text/xml'
        test.equals body, '<?xml version="1.0" encoding="UTF-8" ?>\n<Response>\n<Say>Thanks for your text, we\'ll be in touch.</Say>\n</Response>'
        test.done()
    

    并使用

    从另一个文件(可能是您的Cakefile)运行它
    {reporters} = require 'nodeunit'
    reporters.default.run ['test']
    

    【讨论】:

      【解决方案2】:

      测试很简单。您只需创建一个单元测试来启动您的快速服务器,进行 http POST 并断言您的 HTTP 帖子可以正常工作并返回正确的输出。

      使用vows-is。 (抱歉,没有咖啡脚本)

      var is = require("vows-is"),
          app = require("../src/app.js");
      
      is.config({
          "server": {
              "factory": function _factory(cb) { cb(app); }
          }
      });
      
      is.suite("http request test").batch()
      
          .context("a request to POST /incoming")
              // make a POST request
              .topic.is.a.request({
                  "method": "POST",
                  "uri": "http://localhost:8080/incoming",
                  // set the request body (req.body)
                  "json": {
                      "Body": ...,
                      "From": ...
                  }
              })
              .vow.it.should.have.status(200)
              .vow.it.should.have
                  .header("content-type", "text/xml")
              .context("contains a body that")
                  .topic.is.property('body')
                  .vow.it.should.be.ok
                  .vow.it.should.include.string('<?xml version="1.0" encoding="UTF-8" ?>\n<Response>\n<Say>Thanks for your text, we\'ll be in touch.</Say>\n</Response>')
      
      // run the test suite
      .suite().run({
          reporter: is.reporter
      }, function() {
          is.end();
      });
      

      将其存储在文件夹test 中的文件http-test.js 中。然后运行

      $ npm install vows-is
      $ node test/http-test.js
      

      查看example of exporting your serverSetup function

      【讨论】:

      • 改变 cb(serverSetup());到 cb(serverSetup);使代码与 Express 一起正常工作!谢谢
      • @donald 那是因为cb 当前接受undefined 作为有效参数。这是未记录的,不是 API 的一部分。这在未来版本的 vows-is 中可能会或可能不会改变。它目前有效,但不是面向未来的(使用风险自负!)
      • 但它不适用于“()”。我的 server.js 中有 app = module.exports = express.createServer()
      • @donald 哦,我明白了。这也将起作用。然后不是serverSetup,而是app。更新了问题。
      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      相关资源
      最近更新 更多