Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 测试路由并在其中存根函数调用?_Node.js_Express_Mocha.js_Sinon_Chai - Fatal编程技术网

Node.js 测试路由并在其中存根函数调用?

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

是否可以使用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: 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' ] });
参考:

  • (键入脚本,但原因仍然有道理)

谢谢,我今天晚些时候会尝试。好的,告诉我是否成功。很抱歉通过这种方式与您联系(我的评论与您的输入无关)。我遇到了一个你在“分类”中投票的问题,你做了错误的选择。请:仔细研究分类帮助,避免将不属于那里的项目放入编辑队列。我希望你们能把这看作是一个提高投票率的机会。我具体是说。如果您有进一步的问题或反馈,请随时给我留言。如果你给我一个快速的答复,我会立即删除这里的评论。