Testing post in bare Node.js app with Mocha and Superagent

久未见 提交于 2019-12-08 09:18:27

问题


I hope the day finds you well.

So I'm trying to build some TDD chops in Node and to that end I've built a super bare bones app which runs a simple GET and POST request. All it does is serve the worlds most simple form, then take what the user enters into this form and put it on the screen. This is virgin Node, no frameworks involved. I'm using Mocha and Superagent for testing and am getting stuck on the POST test. Here's my app:

    var http = require('http');
    var qs = require('querystring');

    var server = http.createServer(function(req, res){
        switch(req.method){
            case 'GET':
                console.log("Calling get");
                res.statusCode = 200;
                res.setHeader('Content-Type', 'text/html');
                res.end("<p>Hello World!</p>" + 
                    "<form method='post' action='/'>" +
                    "<input type='text' name='field'>" + 
                    "<input type='submit'>" +
                    "</form>");
                break;
            case 'POST':
                var body = "";
                req.on('data', function(data){
                    body += data;
                })
                req.on('end', function(){
                    var post = qs.parse(body);
                    res.statusCode = 200;
                    res.setHeader('Content-Type', 'text/html');
                    res.end("<p>" + post.field + "</p>")
                    // console.log(req)
                    console.log(post);
                });
        }
    })
    server.listen(8080);

    console.log('Server running on port 8080.')

And here are my tests:

    var request = require('superagent');
    var expect = require('expect.js');

    describe('Main page', function(){
        it("should get 'Hello World!'", function(done){
            request.get('localhost:8080').end(function(res){
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.text).to.contain("World");
                done();
            });
        });

        it("should display input text from a form.", function(done){
            request.post('localhost:8080')
                .send({field: "Test string."})
                .end(function(res){
                    expect(res).to.exist;
                    expect(res.status).to.equal(200);
                    expect(res.text).to.contain("Test");
                    done();
            })
        });
    });

I like to keep it as simple as possible when I'm learning things so that I can isolate what I'm doing. From what I know of Superagent the .send() method should take an object which contains the various post keys and values, this is then passed to the app and run along the given route. But when I run the test everything passes except the expect(res.text).to.contain("Test") assertion. I get an error that Mocha expected '

undefined

' to contain 'Test'. When I just start the app and run it in browser, everything is good.

I've been wrestling with this for a while and now I'm going to the hivemind. As I mentioned I'm a TDD newbie but I want to be a testing god and this is really harshing my mellow. Any enlightenment would be greatly appreciated.


回答1:


And got it on my own. Amazing what documentation can teach when you stare at it long enough.

After looking at the docs located here:

http://visionmedia.github.io/superagent/#post-/%20put%20requests

I realized that for Superagent to post properly I had to tell it the type of post being made prior to the information being sent like so:

    it("should display input text from a form.", function(done){
        request.post('localhost:8080')
            .type('form')
            .send({field: "Test string."})
            .end(function(res){
                expect(res).to.exist;
                expect(res.status).to.equal(200);
                expect(res.text).to.contain("Test");
                done();
        })
    });

Nifty stuff. Hope others find this helpful.



来源:https://stackoverflow.com/questions/25715078/testing-post-in-bare-node-js-app-with-mocha-and-superagent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!