使用Mocha和Superagent在裸Node.js应用程序中测试帖子 [英] Testing post in bare Node.js app with Mocha and Superagent

查看:102
本文介绍了使用Mocha和Superagent在裸Node.js应用程序中测试帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望那一天能找到你。

I hope the day finds you well.

所以我正在尝试在Node中构建一些TDD chops,为此我建立了一个超级裸骨运行简单的GET和POST请求的应用程序。它所做的只是提供世界上最简单的形式,然后将用户输入的内容放入此表单并将其放在屏幕上。这是原始节点,不涉及框架。我正在使用Mocha和Superagent进行测试,并且在POST测试中遇到困难。这是我的应用程序:

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.')

以下是我的测试:

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

我喜欢在学习东西时保持尽可能简单,以便我可以隔离什么我正在做。根据我对Superagent的了解,.send()方法应该包含一个包含各种post键和值的对象,然后将其传递给app并沿给定路径运行。但是当我运行测试时,除了expect(res.text).to.contain(Test)断言之外,所有内容都会通过。我得到一个错误,Mocha期望'

undefined

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

我一直在努力解决这个问题,现在我要去看看hivemind 。正如我所提到的,我是一名TDD新手,但我想成为一名测试之神,这真的让我感到沮丧。任何启蒙都会受到高度赞赏。

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.

推荐答案

并且我自己得到了它。令人惊讶的是,当你长时间盯着它时,文档可以教什么。

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

我意识到,对于Superagent要正确发布,我必须告诉它在发送信息之前发布的帖子类型如下:

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.

这篇关于使用Mocha和Superagent在裸Node.js应用程序中测试帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆