コマンドからの実行環境は、以下を御覧ください。
MacにMochaをインストールしてみた:
http://madroom-project.blogspot.jp/2013/03/macmocha.html
同じテストをコマンドとブラウザの両方から実行してみました。
(1) MochaをDL、"mocha"というディレクトリ名で解凍
https://github.com/visionmedia/mocha
(2) Mochaのディレクトリに並べる形で"tests"ディレクトリを作成
(3) tests/test_sample.jsを作成
describe('Test Name', function() {
it('1 + 1 = 2', function() {
var expected = 2;
var actual = 1 + 1;
assert.equal(expected, actual);
});
});
(4) tests/index.htmlを作成、アクセス<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="../mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="../mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test_sample.js"></script>
<script>
var assert = function(expr, msg) {
if (!expr) throw new Error(msg || 'failed');
};
assert.equal = function(a, b, msg) {
if (a != b) throw new Error(msg || ('failed : '+a+','+b));
};
$(function() {
mocha.run();
});
</script>
</body>
</html>
assert = require('assert');
require('./test_sample');
(6) コマンドから実行
$ mocha --reporter spec tests/
Test Name
✓ 1 + 1 = 2
1 test complete (2 ms)

No comments:
Post a Comment