简体   繁体   English

用于单元测试POST请求的npm软件包是什么?

[英]What npm package to use for unit testing POST request?

It will have to have the option to set the req.params of POST request. 它必须具有设置POST请求的req.params的选项。

supertest package doesn't provide this or it is not well documented to be clear it does. supertest软件包不提供此功能,或者没有明确的文档证明它确实提供了此功能。

If you are testing an express application, supertest supports POST as well as most other HTTP methods. 如果您要测试快速应用程序,则supertest支持POST以及大多数其他HTTP方法。 The express req.params is populated by the path portion of the URL, so you would do something like 快速的req.params由URL的路径部分填充,因此您将执行以下操作

var app = require('./my-app')
var request = require('supertest')(app)

request.post('/some/path/req/params/is/here?some=query')
  .send({some: 'body'})
  .expect(200)
  .end(function (error, res) {
    assert(res.body.something === 'value')
  })

Yes you can with super test, since it has the same actions as superagent , this is an example: 是的,您可以使用超级测试,因为它具有与superagent相同的操作,这是一个示例:

request
  .post('/api/pet')
  .send({ name: 'Manny', species: 'cat' })
  .set('X-API-Key', 'foobar')
  .set('Accept', 'application/json')
  .end(function(err, res){
    // Calling the end function will send the request 
  });

https://github.com/visionmedia/supertest https://github.com/visionmedia/supertest

By default sending strings will set the Content-Type to application/x-www-form-urlencoded, multiple calls will be concatenated with &, here resulting in name=tj&pet=tobi: 默认情况下,发送字符串会将Content-Type设置为application / x-www-form-urlencoded,多个调用将与&串联,从而导致name = tj&pet = tobi:

request.post('/user')
    .send('name=tj')
    .send('pet=tobi')
    .end(callback);

The supertest docs say: 超级测试文档说:

Anything you can do with superagent, you can do with supertest 超级代理可以做的任何事情,超级测​​试可以做的一切

Therefore, the following should work fine: 因此,以下应正常工作:

request(app)
.post('/')
.query({format: 'json'})
.expect(....)

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

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