简体   繁体   English

POST无法用于Express REST API的Mocha / Chai测试

[英]POST not working for Mocha/Chai tests on Express REST API

I'm having a problem with Mocha tests on my Express app. 我在Express应用程序上的Mocha测试遇到问题。 I have a REST API set up that I know works in the browser, but it's getting a bit large and therefore manual testing has become tedious. 我有一个REST API设置,我知道它可以在浏览器中工作,但是它变得有些大,因此手动测试变得乏味。

Anyway, all my GET tests work just fine, but when I add tests for my POST requests, they fail: 无论如何,我所有的GET测试都可以正常工作,但是当我为POST请求添加测试时,它们将失败:

 Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 400
  at testPostSingle (test-app.js:297:21)
  at test-app.js:195:21
  at Test.Request.callback (/home/jacobd/healthboard/node_modules/chai-http/node_modules/superagent/lib/node/index.js:603:3)
  at Stream.<anonymous> (/home/jacobd/healthboard/node_modules/chai-http/node_modules/superagent/lib/node/index.js:767:18)
  at Unzip.<anonymous> (/home/jacobd/healthboard/node_modules/chai-http/node_modules/superagent/lib/node/utils.js:108:12)
  at _stream_readable.js:944:16

I'm using Mocha, with the Chai should library and chai-http for requests. 我使用的是Mocha,Chai应该库和chai-http用于请求。

My relevant code: 我的相关代码:

models.forEach(function(model, i) {

    var url = '/api/v1/' + model;
    var list = lists[i];

    /****************************** API POST TESTS ******************************/

    describe(util.format('API: /%s POST', model), function() {
        var minArgsObj = {
            title: 'Test ' + model + ' Title'
        };

        // Initialize list at start
        before(function(done) {
            instances._init(function(err) {
                if (!err) done();
            });
        });

        // Nuke list before each test
        beforeEach(function(done) {
            list.clear();
            done();
        });

        // Make sure POST single object works
        it('should create and add model on ' + url + ' with minimal arguments', function(done) {
            var req = {};
            req.options = _.clone(minArgsObj);
            chai.request('server')
                .post(url)
                .send(req)
                .end(function(err, res) {
                    testPostSingle(res, minArgsObj);
                    list.list.length.should.equal(1);
                    res.body.should.eql(list.list[0]);
                    done();
                });
        });
    });
});


function testPostSingle(res, ref) {
    res.should.have.status(200);
    ...
}

When I put a log message in the POST route declaration, it doesn't show up, so that tells me that my request is getting stopped before even hitting my server. 当我在POST路由声明中放置一条日志消息时,它没有显示,这表明我的请求在到达服务器之前就已停止。 Maybe the route isn't getting mounted properly in Mocha? 也许路线没有在摩卡咖啡中正确安装? It works when I make the request outside of Mocha, but I can't figure out why it won't work in a test environment. 当我在Mocha之外发出请求时,它可以工作,但我不知道为什么它不能在测试环境中工作。

Thanks in advance for your help, and let me know if you need any more info! 预先感谢您的帮助,如果您需要更多信息,请告诉我!

The relevant code is in testPostSingle. 相关代码在testPostSingle中。 Your call is returning 400 bad request. 您的电话返回了400个错误请求。 Are you sure the route is correct or is set up to handle POST as well as GET? 您确定路由正确还是已设置为可以处理POST和GET? Are you sure parameters are correct? 您确定参数正确吗? Make testPostSingle print out the body of the HTTP response so you know the details. 使testPostSingle打印出HTTP响应的主体,以便您了解详细信息。 You can also add some debug code in the route for that request on your server. 您也可以在服务器上为该请求的路由中添加一些调试代码。

I'm terrible and issue is very simple: 我很糟糕,问题很简单:

Instead of chai.request('server') 代替chai.request('server')

It needs to be chai.request(server) 它必须是chai.request(server)

Thanks to Jason Livesay for pointing me at the right line! 感谢Jason Livesay向我指出正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM