1

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!

2 Answers 2

2

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

changing cb(serverSetup()); to cb(serverSetup); makes the code working just fine with Express! thanks
@donald that's because cb accepts undefined as a valid parameter currently. This is undocumented and not part of the API. This will may or may not change in future versions of vows-is. It works currently but is not future-proof (use at own risk!)
but it doesn't work with "()". I have app = module.exports = express.createServer() in my server.js
@donald Oh I see. That will work as well. Then it's not serverSetup but app instead. Updated the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.