本文介绍了supertest Expect(function(res){}),错误:预期的[Function]响应正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用supertest和mocha测试我的Express Rest API.有一个测试用例,我想用supertest:expect(function(res()){})方法检查返回的响应主体.但是我遇到一个我无法弄清原因的错误:

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

});

推荐答案

在supertest的0.9.0版本中添加了将函数传递给.expect()的功能,该版本目前是最新版本.

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.

这些是有问题的提交: https://github.com/visionmedia/supertest/commit /00dad1bf84896f8a610b028dcbd81ce2e53779fb https://github.com/visionmedia/supere4e49473974 /a>

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

只需更新supertest程序包即可.

Just update the supertest package and it should work.

这篇关于supertest Expect(function(res){}),错误:预期的[Function]响应正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!