supertest expect(function(res){}) , Error: expected [Function] response body ??

孤者浪人 提交于 2020-01-25 12:02:08

问题


I'm using supertest and mocha testing my express rest api. there's this test case I want to check the returned response body with a method of supertest:expect(function(res){ } ). But I'm facing an error that I can't figure out why:

Error: expected [Function] response body, got '{"name":"Aaron Shen","_id":"
530ed1ce92788ed031022d8c","__v":0,"active":true}'

Does anybody know how to fix? below is my testing code:

it('should return correct player',function(done){

    var url = '/api/players/' + pid;
    request(app)
        .get(url)
        .expect(200)
        .expect(function(res){
            res.body.should.have.property('name');
        })
        .end(done);

});

回答1:


The ability to pass a function to .expect() was added in version 0.9.0 of supertest, which as of now is the latest version.

These are the commits in question: https://github.com/visionmedia/supertest/commit/00dad1bf84896f8a610b028dcbd81ce2e53779fb, https://github.com/visionmedia/supertest/commit/a8e5596cc94e97e2b937792853c498cae4ca6764

Just update the supertest package and it should work.



来源:https://stackoverflow.com/questions/22060161/supertest-expectfunctionres-error-expected-function-response-body

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