在Supertest上使用PUT方法

问题描述 投票:1回答:2

如何在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
2个回答
2
投票

添加:

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

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

    });

现在它有效(?)


1
投票

让我在这里分享一个例子,使用promises,不需要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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.