2012-05-03 19 views
0

为了让我的摩卡测试知道我的课程正在测试中(用于在浏览器中运行Mocha),有什么约定?由于两者都包裹在闭合,也不是在全球范围内...使用Mocha在浏览器中测试CS类?

Can't find the Monkey class due to the fact it's not global, I think...


monkey.spec.coffee

describe "Monkey", -> 
    it "adds two to the given number", -> 
    expect Monkey.add2(4).to.equal(6) 

monkey.coffee

class Monkey 

test_runner.html

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Mocha Test Runner</title> 
    <meta charset="utf8"> 
    <link rel="stylesheet" href="mocha.css"> 
    <script src="mocha.js"></script> 
    <script src="expect.js"></script> 
    <script> 
     mocha.setup('bdd'); 
    </script> 

    <!-- Load in files under test --> 
    <script src="monkey.js"></script> 

    <!-- Load in spec files --> 
    <script src="monkey.spec.js"></script> 
    </head> 
    <body> 
    <div id="mocha"></div> 

    <script> 
     mocha.run(); 
    </script> 
    </body> 
</html> 
+0

[CoffeeScript中定义的类未在Jasmine规范中找到]的可能重复(http://stackoverflow.com/questions/8310329/classes-defined-in-coffeescript-not-found-by-jasmine-specs) –

回答

2
class Monkey 

创建在该文件中限定范围Monkey类。你想

class window.Monkey 

class @Monkey 

的简称。

+0

Won '有没有使'猴'全球的影响?我不一定希望猴子是全球性的,但我希望我的测试能够知道它的存在......或者我不正确地理解这一点吗? – neezer

+0

由于基于浏览器的JavaScript没有链接器,因此在单个文件之外创建变量的唯一方法(假定每个文件都有一个包装器)就是让它在任何地方都可见。如果您处于测试模式,您可以添加代码,例如只导出'Monkey'。 –

+0

陷阱,有道理。感谢您的澄清。 – neezer

相关问题