Javascript 使用Mocha在浏览器中测试CS类?
让我的mocha测试知道我的被测类(在浏览器中运行mocha)的约定是什么?因为两者都是用闭包包装的,所以都不在全局范围内Javascript 使用Mocha在浏览器中测试CS类?,javascript,tdd,coffeescript,mocha.js,Javascript,Tdd,Coffeescript,Mocha.js,让我的mocha测试知道我的被测类(在浏览器中运行mocha)的约定是什么?因为两者都是用闭包包装的,所以都不在全局范围内 monkey.spec.coffee describe "Monkey", -> it "adds two to the given number", -> expect Monkey.add2(4).to.equal(6) class Monkey 猴子咖啡 describe "Monkey", -> it "adds two t
monkey.spec.coffee
describe "Monkey", ->
it "adds two to the given number", ->
expect Monkey.add2(4).to.equal(6)
class Monkey
猴子咖啡
describe "Monkey", ->
it "adds two to the given number", ->
expect Monkey.add2(4).to.equal(6)
class Monkey
test\u 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>
摩卡跑鞋
摩卡咖啡设置(“bdd”);
mocha.run();
创建一个作用域在文件中的Monkey
类。你想要
class window.Monkey
或
简言之。可能的复制不会产生使
Monkey
global的效果吗?我不一定希望Monkey
是全局的,但我确实希望我的测试知道它的存在。。。或者我没有正确理解这一点?因为基于浏览器的JavaScript没有链接器,所以使变量在单个文件之外可见的唯一方法(假设每个文件都有一个包装器)是使其在任何地方都可见。如果您处于测试模式,您可以将代码添加到,例如,仅导出Monkey
。明白了,这很有意义。谢谢你的澄清。