【问题标题】:Testing express routes with mongoose query使用猫鼬查询测试快递路线
【发布时间】:2017-03-16 20:08:50
【问题描述】:

我正在尝试测试包含猫鼬查询的路线。我不断回复:

AssertionError: expected undefined to equal true

以下是我的测试的基本模板。目前我只想确认它调用了 res.json。

路由返回用户模型中的所有条目

route.js

const User = require('../../../models/users/users');

const listUsers = (req, res) => {
  User.find((err, users) => {
    if (err) res.send(err);

    res.json(users);
  })
};

module.exports = listUsers;

test.js

const expect = require('chai').expect;
const sinon = require('sinon');

const listUsers = require('../../../../src/routes/api/users/listUsers');

describe('listUsers', () => {
  it('retrieves users', () => {
    const req = {};
    const res = {};
    const spy = res.json = sinon.spy;

    listUsers(req, res);
    expect(spy.calledOnce).to.equal(true);
  })
});

【问题讨论】:

    标签: javascript node.js express mongoose


    【解决方案1】:

    find 函数有两个参数。第一个是搜索条件,第二个是回调函数。看起来你错过了第一个参数。

    由于您想查找所有用户,因此您的标准是 - {}

    所以这会解决你的问题 -

    User.find({}, (err, users) => {
        if (err) res.send(err);
    
        res.json(users);
      })
    

    【讨论】:

      猜你喜欢
      • 2017-02-27
      • 2021-04-11
      • 1970-01-01
      • 2013-05-24
      • 2012-02-03
      • 1970-01-01
      • 2011-08-28
      • 2013-12-04
      • 2019-05-23
      相关资源
      最近更新 更多