Javascript 用mongoose查询测试快速路线
我正在尝试测试我的路线中有猫鼬查询。我不断回来:Javascript 用mongoose查询测试快速路线,javascript,node.js,express,mongoose,Javascript,Node.js,Express,Mongoose,我正在尝试测试我的路线中有猫鼬查询。我不断回来: AssertionError: expected undefined to equal true 下面是我测试的基本模板。现在我只想确认它调用了res.json 路由返回用户模型中的所有条目 route.js const User = require('../../../models/users/users'); const listUsers = (req, res) => { User.find((err, users) =&g
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);
})
});
find函数采用两个参数。第一个是搜索条件,第二个是回调函数。看起来您错过了第一个参数 因为您想找到所有用户,所以您的标准是-{} 所以这会解决你的问题-
User.find({}, (err, users) => {
if (err) res.send(err);
res.json(users);
})
find函数采用两个参数。第一个是搜索条件,第二个是回调函数。看起来您错过了第一个参数 因为您想找到所有用户,所以您的标准是-{} 所以这会解决你的问题-
User.find({}, (err, users) => {
if (err) res.send(err);
res.json(users);
})