Node.js 测试路由并在其中存根函数调用?
是否可以使用Mocha和Sinon在快速路线中存根功能 下面是实现,在Node.js 测试路由并在其中存根函数调用?,node.js,express,mocha.js,sinon,chai,Node.js,Express,Mocha.js,Sinon,Chai,是否可以使用Mocha和Sinon在快速路线中存根功能 下面是实现,在/apps/stuff/controller.js import db from '../lib/database'; const getStuff = async (req, res) => { const results = await db.query(req.id); // I want to stub this return res.status(200).json({ thingy: res
/apps/stuff/controller.js
import db from '../lib/database';
const getStuff = async (req, res) => {
const results = await db.query(req.id); // I want to stub this
return res.status(200).json({
thingy: results.thingy,
stuff: [
results.foo,
results.bar,
],
});
};
export default {
getStuff,
};
import stuff from './apps/stuff/controller';
import express from 'express';
const app = express();
app.route('/stuff')
.get(stuff.getStuff);
在/routes.js
import db from '../lib/database';
const getStuff = async (req, res) => {
const results = await db.query(req.id); // I want to stub this
return res.status(200).json({
thingy: results.thingy,
stuff: [
results.foo,
results.bar,
],
});
};
export default {
getStuff,
};
import stuff from './apps/stuff/controller';
import express from 'express';
const app = express();
app.route('/stuff')
.get(stuff.getStuff);
因此,在测试用例中,我希望在GET/stuff
请求测试运行期间,存根调用db.query()
,并返回一个自定义结果
// ./test/stuff/controller.js
import db from '../../apps/lib/database';
import chai from 'chai';
import chaiHttp from 'chai-http';
import server from '../../index';
const { expect } = chai;
chai.use(chaiHttp);
describe('getStuff', () => {
it('gets you stuff', async () => {
// I have tried this, but it results in TypeError: Cannot stub non-existent own property query
// I presume it is creating a new "empty" object instead of stubbing the actual implementation
sandbox.stub(db, 'query').resolves({ thingy: 'bar', stuff: [ 123, 'wadsofasd' ] });
chai.request(server)
.get('/stuff?id=123')
.then(res => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
thingy: 'bar',
stuff: [
123,
'wadsofasd',
]
});
});
});
});
在上述场景中,存根/模拟db.query调用的正确方法是什么?我在网上搜索了几个小时,但没有发现一个类似案例的有效版本。您是否尝试过
proxyquire
Proxyquire是一个javascript库,用于欺骗node.js require,以便在检测到require('example/route/of/your/import')时可以将另一个函数(存根)传递给它
这样你就可以写作了
const proxyquire = require('proxyquire');
proxyquire('./apps/stuff/controller', {
getStuff: sandbox.stub()
}
除了使用proxyquire之外,还可以选择避免使用导入默认值 您的
db
模块看起来像是在使用导出默认值
(它不是作为符号导出的),并且使sinon无法正确地将其存根
解决方案使用命名导出
database.js
export function query() {
// implementation
}
=== OR ===
function query() { .. }
module.exports = {
query
}
import * as db from '../lib/database'; // alternative => import { query } from '...'
const getStuff = async (req, res) => {
const results = await db.query(req.id); // I want to stub this
// ...
};
import * as db from '../../apps/lib/database';
// ...
sandbox.stub(db, 'query').resolves({ thingy: 'bar', stuff: [ 123, 'wadsofasd' ] });
stuff/controller.js
export function query() {
// implementation
}
=== OR ===
function query() { .. }
module.exports = {
query
}
import * as db from '../lib/database'; // alternative => import { query } from '...'
const getStuff = async (req, res) => {
const results = await db.query(req.id); // I want to stub this
// ...
};
import * as db from '../../apps/lib/database';
// ...
sandbox.stub(db, 'query').resolves({ thingy: 'bar', stuff: [ 123, 'wadsofasd' ] });
test/stuff/controller.js
export function query() {
// implementation
}
=== OR ===
function query() { .. }
module.exports = {
query
}
import * as db from '../lib/database'; // alternative => import { query } from '...'
const getStuff = async (req, res) => {
const results = await db.query(req.id); // I want to stub this
// ...
};
import * as db from '../../apps/lib/database';
// ...
sandbox.stub(db, 'query').resolves({ thingy: 'bar', stuff: [ 123, 'wadsofasd' ] });
参考:
- (键入脚本,但原因仍然有道理)