【发布时间】:2014-06-10 13:09:06
【问题描述】:
我正在评估 mocha,但我无法解决一些基本问题,我编写了一个示例测试,我想在 node.js 和使用 html 文件的浏览器中运行它,但我找不到方法只写一个对两者都适用的测试,如果我在测试文件中添加要求,这对 node.js 来说很好,我在浏览器中得到“未捕获的 ReferenceError:要求未定义”,删除要求我在节点 js 中得到“chai 未定义”
这是代码
(function(exports) {
"use strict";
function Cow(name) {
this.name = name || "Anon cow";
}
exports.Cow = Cow;
})(this);
这是测试
var chai = require('chai'),
cowobj = require ("../cow"),
expect = chai.expect;
describe("Cow", function() {
describe("constructor", function() {
it("should have a default name", function() {
var cow = new cowobj.Cow();
expect(cow.name).to.equal("Anon cow");
});
});
这是html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cow tests</title>
<link rel="stylesheet" media="all" href="node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"><p><a href=".">Index</a></p></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script src="cow.js"></script>
<script>mocha.setup('bdd')</script>
<script src="./test/cow_test.js"></script>
<script>mocha.run();</script>
</body>
</html>
关于如何解决这个问题的任何想法?
【问题讨论】:
标签: javascript testing mocha.js chai