简体   繁体   English

在Mocha ESlint错误中测试内部功能

[英]Testing internal functions in Mocha ESlint error

I'm currently developing an Nodejs application and carrying out some unit tests (I'm using Mocha, Chai and Sinon). 我目前正在开发Nodejs应用程序并进行一些单元测试(我正在使用Mocha,Chai和Sinon)。

I ran into a little ESlint error when I exported and tested an internal function. 导出和测试内部函数时,遇到了一个ESlint错误。

function _buildPayload(){
    //....
}

module.exports = { _buildPayload };

Then in my test script 然后在我的测试脚本中

const {_buildPayload} = requires('./someModule')

describe('Test',function(){
          it('Should work',function(){
              let expected = _buildPayload();
             })
        })

When I write the let expected = _buildPayload(); 当我写的时候let expected = _buildPayload(); ESlint returns the following error: ESlint返回以下错误:

error  Shouldn't be accessing private attribute '_buildPayLoad'

My question is should I change the name of my function to not represent and internal even though it is? 我的问题是我是否应该将函数的名称更改为不表示内部,即使是内部?

@philipisapain makes a good point that testing internal methods may not be necessary. @philipisapain很好地指出,可能不需要测试内部方法。 If you do need to do it, you have a couple options: 如果确实需要这样做,则有两种选择:

  1. Disable the rule by placing /* eslint-disable rule-name */ at the top of any test scripts that call private methods. 通过在所有调用私有方法的测试脚本的顶部放置/* eslint-disable rule-name */禁用该规则。
  2. Disable the rule in all test scripts using a glob config in your .eslintrc , provided you're using at least ESLint v4.1.0 : 假设您至少使用ESLint v4.1.0 ,请在.eslintrc使用glob配置在所有测试脚本中禁用该规则:

     "overrides": [{ "files": ["*.test.js"], "rules": [{ "rule-name": "off" }] }] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM