Node.js:如何测试函数

发布于 2024-12-03 08:43:02 字数 491 浏览 2 评论 0 原文

我如何测试这样的功能?

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

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

谢谢!

How can I test a function like this?

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

I haven't chosen any test framework yet. I don't understand how this can be tested.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-12-10 08:43:02

我更喜欢 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']

I prefer the lighter-weight syntax of nodeunit, combined with request for making HTTP requests. You'd create a test/test.coffee file that looks something like

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()

and run it from another file (perhaps your Cakefile) with

{reporters} = require 'nodeunit'
reporters.default.run ['test']
ゃ懵逼小萝莉 2024-12-10 08:43:02

测试很简单。您只需创建一个单元测试来启动您的 Express 服务器,进行 http POST 并断言您的 HTTP post 有效并获取正确的输出。

使用 vows-is。 (抱歉,没有 CoffeeScript)

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

查看导出 serverSetup 函数的示例

Testing is simple. You just create a unit test that starts your express server, makes a http POST and asserts that your HTTP post works and gets the correct output back.

Using vows-is. (Sorry, no coffeescript)

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

Store this in a file http-test.js in a folder test. Then just run

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

See an example of exporting your serverSetup function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文