-2
我使用Chrome擴展的生成器創建了一個項目。以下是我的文件夾結構的表示。我想測試common.js
。我的摩卡的理解是,我應該有一個index.html
文件是這樣的:摩卡測試,Yeoman,Chrome擴展
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Spec Runner</title>
<link rel="stylesheet" href="bower_components/mocha/mocha.css">
</head>
<body>
<div id="mocha"></div>
<script src="bower_components/mocha/mocha.js"></script>
<script>
mocha.setup('bdd');
mocha.reporter('html');
</script>
<script src="bower_components/chai/chai.js"></script>
<script>
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();
</script>
<!-- include source files here... -->
<script src="../app/scripts/common.js"></script>
<!-- include spec files here... -->
<script src="spec/test.js"></script>
<script>mocha.run()</script>
</body>
</html>
的測試文件(test.js
)看起來是這樣的:
/* global describe, it */
describe('Find the root recipe node', function() {
it('Should not find an elem in null dom', function() {
console.log(dds_hmr.findRecipeRoot);
});
});
的common.js
文件看起來是這樣的:
var dds_hmr = {};
dds_hmr.findRecipeRoot = function (elem) {
if (elem && elem.hasAttribute("itemtype")
&& elem.getAttribute("itemtype") === "http://schema.org/Recipe") {
return [elem];
} else {
var result = [];
if (elem) {
for (var i = 0; i < elem.childNodes.length; i++) {
var child = dds_hmr.findRecipeRoot(elem.childNodes[i]);
result.concat(child);
}
}
return result;
}
};
這是我得到的錯誤:
1) Find the root recipe node Should not find an elem in null dom:
ReferenceError: Can't find variable: dds_hmr
at http://localhost:9000/spec/test.js:4
at http://localhost:9000/bower_components/mocha/mocha.js:4263
at http://localhost:9000/bower_components/mocha/mocha.js:4635
at http://localhost:9000/bower_components/mocha/mocha.js:4694
at next (http://localhost:9000/bower_components/mocha/mocha.js:4561)
at http://localhost:9000/bower_components/mocha/mocha.js:4570
at next (http://localhost:9000/bower_components/mocha/mocha.js:4514)
at http://localhost:9000/bower_components/mocha/mocha.js:4538
at timeslice (http://localhost:9000/bower_components/mocha/mocha.js:5531)
我需要做什麼才能使測試引用common.js
文件中的代碼?
對於那些反對投票,請解釋您的問題與問題或答案。據我所知,這是兩種模式。 – Virmundi 2015-02-10 12:45:03
我認爲在您使用正確的信息編輯問題之前積累了積分。和你的答案一樣。 – Xan 2015-02-10 13:04:09