2012-05-03 21 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

相關問題