运行mocha xxx.js后里面的document为undefined,怎么得打正确的document?
这个问题的描述:
http://stackoverflow.com/questions/10091025/defining-window-for-testing-in-mocha
mocha xxx.js
是在node下运行的,node中没有window变量。
可以试试在浏览器中运行mocha
mocha init xxx
运行上面命令,就会在xxx目录下生成index.html文件。
类似于
<!DOCTYPE html>
<html>
<body>
<h1>Unit.js tests in the browser with Mocha</h1>
<div id="mocha"></div>
<script src="mocha.js"></script>
<script>
mocha.setup('bdd');
</script>
<script src="tests.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
再讲要测试的文件引入到html文件中,应该就可以访问window变量了吧。
以上是我的猜测,没有实际尝试,谢谢