【问题标题】:Using PUT method on Supertest在 Supertest 上使用 PUT 方法
【发布时间】:2017-03-05 13:05:53
【问题描述】:

如何在 SuperTest 中使用 PUT 方法?我得到的只是“404 Not found”作为响应。

请求处理程序:

router.put('/', function (req, res) {
    res.type('json');

    FooResource(req.body, function () {
        res.send("{}");
    });
});

测试套件:

describe("PUT /foo/fii", function () {

    it("Respond with 200", function (done) {

        request(app)
            .put('/')
            .set('Accept', 'application/json')
            .expect(200, done);

    });
});

【问题讨论】:

    标签: javascript node.js testing supertest http-put


    【解决方案1】:

    添加:

        it("Respond with 200", function (done) {
    
            request(app)
                .put('/')
                .send("{}")
                .expect(200)
                .end(function(err, res) {
                    done();
                })
    
        });
    

    现在它可以工作了(?)

    【讨论】:

    • 它应该是done(err).end(done)。否则它不会验证 statusCode
    【解决方案2】:

    让我在这里分享一个示例,使用承诺,不需要done()

    describe('PUT: update task (id:5)', function() {
        test('It should return response 200.', function() {
            return request(app)
                .put('/api/v1.0/tasks/5')
                .send({title:'Code Refactor API',user:'ivanleoncz'})
                .expect(200);
        });
    });
    

    欲了解更多信息:https://www.npmjs.com/package/supertest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-17
      • 2018-02-27
      • 1970-01-01
      • 2011-12-17
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多