Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 如何使用mocha测试基本的javascript文件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Mocha和Coffeescript / @L_772_0@中遗漏了一些明显的东西.

我在/ static / js /中有一个名为ss.coffee的文件,它很简单,只有一个函数

function sortRowCol(a,b) {
    if (a.r == b.r)
        if (a.c == b.C)
            return 0;
        else if (a.c > b.C)
            return 1;
        else return -1;
    else if (a.r > b.r)
        return 1;
    else return -1;
}

功能正常,但我决定今天需要开始测试这个项目,所以我输入了@L_47_1@mocha测试文件

require "../static/js/ss.coffee"

chai = require 'chai'
chai.should()

describe 'SS',->
    describe '#sortRowCol(a,b)',->
      it 'should have a sorTing function',->
        f = sortRowCol
        debugger
        console.log 'checking sort row'
        f.should.not.equal(null,"didn't find the sortRowCol function")
    describe 'sortRowCol(a,->
      it 'should return -1 when first row is less than second',->
        a = {r: 2,c: "A"}
        b = {r: 1,c: "A"}
        r = sortRowCol a,b
        r.should.equal(-1,"didn't get the correct value")

有些事情是不对的,因为我的结果是:

$ mocha --compilers coffee:coffee-script ./test/ss.coffee -R spec           
 SS                                                                      
   #sortRowCol(a,b)                                                              
     1) should have a sorTing function                                           
   sortRowCol(a,b)                                                              
     2) should return -1 when first row is less than second                      


 × 2 of 2 tests Failed:                                                          

 1) SS #sortRowCol(a,b) should have a sorTing function:                 
    ReferenceError: sortRowCol is not defined

它正确找到文件,因为如果我将其更改为不存在的文件名,它将会出现“无法找到模块”的错误.

我尝试将sortRowCol(a,b)更改为#sortRowCol(a,b),反之亦然,没有帮助.文档(link)并没有真正解释#在那里做了什么,这只是因为某种原因出现在这里的ruby成语吗?

我如何引用ss.coffee文件肯定有问题,但我没有看到它.

解决方法

通过在Node中要求脚本,它将被视为任何其他 module,将sortRowCol隔离为闭包中的本地.该脚本必须使用exports或module.exports使其可用于mocha:

function sortRowCol(a,b) {
    // ...
}

if (typeof module !== 'undefined' && module.exports != null) {
    exports.sortRowCol = sortRowCol;
}
ss = require "../static/js/ss.coffee"
sortRowCol = ss.sortRowCol

# ...

至于…

AFAIK,#通常用于暗示它是一种方法 – 例如,Constructor#methodName.但是,不确定这是否适用于此.

大佬总结

以上是大佬教程为你收集整理的node.js – 如何使用mocha测试基本的javascript文件?全部内容,希望文章能够帮你解决node.js – 如何使用mocha测试基本的javascript文件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。