无法读取未定义的 属性 'get' 以在 mocha 中进行单元测试

Cannot read property 'get' of undefined for unit testing in mocha

我正在尝试使用 mocha 和 chai 对我的文件 routes.js 编写单元测试。我在 routes.js 中定义了一个函数,并定义了一个测试文件,其中包含我的测试用例。

当我 运行 我的测试用例时,它显示错误 TypeError: Cannot read property 'get' of undefined

我的 test.js 密码是

var expect = require('chai').expect;
var chai = require('chai');
var app = ('../api/smartAccount/identifyLoan/getDirectDebitTrans/routes');

describe("Unit testing for function", function(){
    it("Testing the function using mocha", function(done){
        var req = {
            queryString: 101
        };

        var test = app.getUser.get(req);
        expect(test).to.be.an('undefined');
        done();
    }); 
});

我正在通过 routes.js 中的 req.queryString.id 请求。

routes.js的代码是

var config = require("../../../../serverConfig/config");

module.exports = {
   getUser: {
        get: function(req, parameters, environment) {

        var id = req.queryString.id;

         //functionality
         });
  }
 }

请帮助我哪里出错了。

p.s get 函数正在从数据库中获取数据,但我没有提到 bd 连接,因为我觉得它在这里无关紧要。 TIA

没什么可怕的。您在 test.js

中缺少 require 关键字

var app = ('../api/smartAccount/identifyLoan/getDirectDebitTrans/routes');

因此;

var expect = require('chai').expect;
var chai = require('chai');
var app = require('../api/smartAccount/identifyLoan/getDirectDebitTrans/routes');

会修复它。

没有 require 关键字,变量 app 只是一个值为 ../api/smartAccount/identifyLoan/getDirectDebitTrans/routes' 的字符串,代码在尝试访问不存在的 get 时终止属性。这显然显示在您的错误消息中 TypeError: Cannot read property 'get' of undefined