gpt4 book ai didi

javascript - Node.js:如何测试函数

转载 作者:行者123 更新时间:2023-11-28 21:17:01 24 4
gpt4 key购买 nike

如何测试这样的函数?

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

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

谢谢!

最佳答案

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

关于javascript - Node.js:如何测试函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7306009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com