node.js - 如何 stub 文件中未声明的对象的实例

标签 node.js mongodb unit-testing strapi

抱歉,我对单元测试有点陌生,所以不确定这是否非常明显,我目前在 Strapi 框架中拥有此代码

'use strict';
const {HOST,PORT,Bucket,cfurl} = require('../../../config/consts');
const strapi             = require('strapi');
const axios              = require('axios');
const NodeCache          = require('node-cache');
const cache              = new NodeCache();

module.exports = {
  getBanner: async (ctx) => {
    let bnr;
    if(ctx.hasOwnProperty('params') && ctx.params.hasOwnProperty('_id') && ctx.params._id != 'undefined'){
      bnr = await Banner.find({stores: ctx.params._id}).limit(1);
    }
    if(!bnr || bnr.length < 1){
      bnr = await Banner.find({"store":{"$exists": false}}).limit(1);
    }  
    ctx.send(JSON.stringify(bnr));
  }
};

我不知道如何去 stub Banner 对象,因为它不是从文件内部声明的。这是我一直在尝试运行的测试,它返回“ReferenceError:横幅未定义”

const sinon = require('sinon');
const proxyquire = require('proxyquire');
const mongoose = require('mongoose');
const mocha = require('mocha');
const assert = require('chai').assert;

describe.only('Unit test: Banner Controller Test', () => {


    describe('Test getBanner', () => {
        let sandbox;
        let fns;
        before((done) => {

            sandbox = sinon.createSandbox();
            var schema = new mongoose.Schema()
            var Banner = mongoose.model('Banner', schema);
            let success = sandbox.stub(Banner, 'find').returns({hello: "there"})

            fns = proxyquire('../../api/banner/controllers/Banner', {
                success
            })
            done()
        })

        it('should return a function call with a JSON object', (done) => {
            fns.getBanner((ctx) => {
                //returns 'ReferenceError: Banner is not defined' when run
            });
            done();
        })
    })



});

任何解决此问题的帮助将不胜感激!

最佳答案

感谢@langgingreflex 插入正确的方向,这是我的工作测试,对那些可能会觉得有帮助的人

describe.only('Unit test: Banner Controller Test', () => {


    describe('Test getBanner', () => {
        let sandbox;
        let fns;
        before((done) => {
            sandbox = sinon.createSandbox();
            var schema = new mongoose.Schema({name: String})
            var Banner = mongoose.model('Banner', schema);
            sandbox.stub(Banner, 'find').callsFake(() => {
                return {
                    limit: sandbox.stub().returns({hello: 'there'})
                }
            })
            global.Banner = Banner;

            fns = proxyquire('../../api/banner/controllers/Banner', {

            })
            done()
        })

        it('should return one a Banner JSON string if passed a param with an _id property', (done) => {
            theObj = {
                send: (data) => {
                    console.log(data)
                }
            }
            fns.getBanner(theObj);
            done();
        })

        after((done) => {
            sandbox.restore();
            done()
        })
    })



});

关于node.js - 如何 stub 文件中未声明的对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58347373/

相关文章:

node.js process.env 无法在命令行中工作

node.js - NODE_ENV=development 在 .bash_profile 中设置但在代码中使用时未定义

python - 使用 mongodb (mongoengine) 和 redis 测试 django

html - 将 Google 测试 XML 报告转换为 HTML(控制台)

c# - 单元测试以捕获 Controller 方法中的异常

java - 在同一对象中测试方法时如何模拟对象中的方法

node.js - 错误 : Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime (72)

node.js - Mongoose 中的多层次人口不起作用

validation - 如何验证 MongoDB 集合中的 DBRefs?

node.js - 更新 Mongoose 架构后,新输入的字段不会插入已创建的集合中