我在Mocha和Coffeescript / Javascript中遗漏了一些明显的东西。
我在/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;
}
该功能正常,但我决定今天需要开始测试这个项目,所以我输入了一个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, b)', ->
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)并没有真正解释#在那里做了什么,这只是因为某种原因出现在这里的红宝石成语吗?
我引用ss.coffee文件的方式一定有问题,但我没有看到。
答案 0 :(得分:11)
通过require
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
。但不确定这是否适用于此。