chai-http 将两个具有相同名称的不同字段视为一个字段

chai-http treats two different fields with the same name as if as one field

我使用 mocha 和 chai-http 来测试 my api server 。问题是它将查询 {stock: 'msft', stock: 'aapl'} 视为 { stock: 'aapl' }.

测试:

var chaiHttp = require('chai-http');
var chai = require('chai');
var assert = chai.assert;
var server = require('../server');

chai.use(chaiHttp);

describe('Functional Tests', function() {

    describe('GET /api/stock-prices => stockData object', function() {

      it('1 stock', function(done) {
       chai.request(server)
        .get('/api/stock-prices')
        .query({stock: 'goog'})
        .end(function(err, res){
          console.log(res.body);
          //complete this one too

          done();
        });
      });

      it('1 stock with like', function(done) {

      });

      it('1 stock with like again (ensure likes arent double counted)', function(done) {

      });

      it('2 stocks', function(done) {
        chai.request(server)
          .get('/api/stock-prices')
          .query({stock: 'msft', stock: 'aapl'})   // <-- THIS ONE
          .end(function(err, res){
            console.log(res.body);
            done();
          });
      });

      it('2 stocks with like', function(done) {

      });

    });

});

结果:

1. Uncaught error outside test suite
Functional Tests
 GET /api/stock-prices => stockData object
{ stockData: { stock: 'GOOG', price: '1019.8300', likes: 14 } }
   / 1 stock (532ms)
   2) 1 stock with like
   3) 1 stock with like again (ensure likes arent double counted)
{ stockData: { stock: 'AAPL', price: '185.7900', likes: 7 } }
   / 2 stocks (360ms)
   4) 2 stocks with like 

输出 { stockData: { stock: 'AAPL', price: '185.7900', likes: 7 } } 只显示一只股票。预计return

{"stockData":[{"stock":"MSFT","price":"104.5600","rel_likes":-5},{"stock":"AAPL","price":"185.7900","rel_likes":5}]}

这不是 chai-http 的错,这是 JavaScript 的错:你不能拥有一个具有两个相同键的对象。

但我认为您也可以将实际的查询字符串传递给 .query():

.query('stock=msft&stock=aapl')