简体   繁体   English

使用Mocha / Chai测试String.prototype.function

[英]Test String.prototype.function Using Mocha/Chai

How could you test a function with Mocha/Chain which is defined as String.prototype.testFunction? 您如何使用Mocha / Chain测试定义为String.prototype.testFunction的函数? For example 例如

Function: 功能:

String.prototype.testFunction = (x, y) => {
  return this.z + x + y
}

module.export = String.prototype.testFunction

Use of Function: 功能使用:

"Text".testFunction(1, 2)

From previous experience from testing functions it should be as follows: 根据测试功能的先前经验,应如下所示:

const app = require("../index.js")

it('description', function(){
  var variable = "Test".app.testFunction(" ", "text")
  variable.should.equal("Test text")
})

Will this work? 这样行吗? Is there a better way to do this? 有一个更好的方法吗?

Thanks 谢谢

Solved. 解决了。 Added function in separate js file and import the function as a constant in the test file 在单独的js文件中添加了功能,并将该功能作为常量导入测试文件中

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

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