Тесты расположены в одном общем файле.
Структура расположения файлов и папок.
css/mocha.css
js/lib/require/require.js
js/lib/require/text.js
js/lib/jquery/jquery.js
js/lib/mocha/mocha.js
js/lib/chai/chai.js
js/lib/sinon/sinon.js
js/lib/sinon/sinon-chai.js
js/index.js
index.html
Код файла index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="css/mocha.css?v=1.0.0" rel="stylesheet" type="text/css" />
<script data-main="js/index.js?v=1.0.0" src="js/lib/require/require.js?v=1.0.0" type="text/javascript"></script>
<title>Тесты</title>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
Код файла index.js
require.config({
paths: {
jquery: 'lib/jquery/jquery'
, mocha: 'lib/mocha/mocha'
, chai: 'lib/chai/chai'
, sinon: 'lib/sinon/sinon'
, sinonChai: 'lib/sinon/sinon-chai'
, text: 'lib/require/text'
}
, shim: {
mocha: {exports: 'mocha'}
, sinon: {exports: 'sinon'}
, sinonChai: {deps: ['chai', 'sinon']}
}
, urlArgs: '_=' + Math.random()
});
require(
[
'jquery'
, 'mocha'
, 'chai'
, 'sinon'
, 'sinonChai'
]
, function(
$
, mocha
, chai
, sinon
, sinonChai
) {
// Тестируемые функции
function sayOK(){
return 'OK';
}
function hello(name, callback) {
callback('Hello ' + name);
}
// Тесты
$.ajaxSetup({cache: false});
mocha.setup('bdd');
var assert = chai.assert
, expect = chai.expect
, should = chai.should(); // Note that should has to be executed
chai.use(sinonChai);
describe('Say OK Tests', function() {
describe('Function sayOK()', function() {
it('should work with assert', function() {
assert.equal(sayOK(), 'OK');
});
it('should work with expect', function() {
expect(sayOK()).to.equal('OK');
})
it('should work with should', function() {
sayOK().should.equal('OK');
});
});
});
describe('Say Hello Tests with Expect', function () {
it('should call callback with correct greeting', function () {
var callback = sinon.spy();
hello('foo', callback);
expect(callback).to.have.been.calledWith('Hello foo');
});
});
describe('Say Hello Tests with Should', function () {
it('should call callback with correct greeting', function () {
var callback = sinon.spy();
hello('foo', callback);
callback.should.have.been.calledWith('Hello foo');
});
});
mocha.run();
}
);
Тесты расположены в отдельных файлах.
Структура расположения файлов и папок.
css/mocha.css
js/lib/require/require.js
js/lib/require/text.js
js/lib/jquery/jquery.js
js/lib/mocha/mocha.js
js/lib/chai/chai.js
js/lib/sinon/sinon.js
js/lib/sinon/sinon-chai.js
js/spec/sample/sample.js
js/index.js
js/tests.js
index.html
Код файла index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="css/mocha.css?v=1.0.0" rel="stylesheet" type="text/css" />
<script data-main="js/index.js?v=1.0.0" src="js/lib/require/require.js?v=1.0.0" type="text/javascript"></script>
<title>Тесты</title>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
Код файла index.js
require.config({
paths: {
jquery: 'lib/jquery/jquery'
, mocha: 'lib/mocha/mocha'
, chai: 'lib/chai/chai'
, sinon: 'lib/sinon/sinon'
, sinonChai: 'lib/sinon/sinon-chai'
, tests: 'tests'
, text: 'lib/require/text'
}
, shim: {
mocha: {exports: 'mocha'}
, sinon: {exports: 'sinon'}
, sinonChai: {deps: ['chai', 'sinon']}
, tests: {deps: ['mocha', 'chai', 'sinon', 'sinonChai']}
}
, urlArgs: '_=' + Math.random()
});
require(
[
'jquery'
, 'mocha'
, 'chai'
, 'sinon'
, 'sinonChai'
, 'tests'
]
, function(
$
, mocha
, chai
, sinon
, sinonChai
, tests
) {
$.ajaxSetup({cache: false});
mocha.setup('bdd');
chai.use(sinonChai);
tests();
mocha.run();
}
);
Код файла tests.js
require.config({
paths: {
sampleTest: 'spec/sample/sample'
}
});
define(
[
'sampleTest'
]
, function(
sampleTest
) {
return function () {
sampleTest();
};
}
);
Кода файла sample.js
require.config({
paths: {
jquery: 'lib/jquery/jquery'
, chai: 'lib/chai/chai'
, sinon: 'lib/sinon/sinon'
}
, shim: {
sinon: {exports: 'sinon'}
}
});
define(
[
'jquery'
, 'chai'
, 'sinon'
]
, function(
$
, chai
, sinon
) {
return function () {
// Тестируемые функции
function sayOK(){
return 'OK';
}
function hello(name, callback) {
callback('Hello ' + name);
}
// Тесты
var assert = chai.assert
, expect = chai.expect
, should = chai.should(); // Note that should has to be executed
describe('Say OK Tests', function() {
describe('Function sayOK()', function() {
it('should work with assert', function() {
assert.equal(sayOK(), 'OK');
});
it('should work with expect', function() {
expect(sayOK()).to.equal('OK');
})
it('should work with should', function() {
sayOK().should.equal('OK');
});
});
});
describe('Say Hello Tests with Expect', function () {
it('should call callback with correct greeting', function () {
var callback = sinon.spy();
hello('foo', callback);
expect(callback).to.have.been.calledWith('Hello foo');
});
});
describe('Say Hello Tests with Should', function () {
it('should call callback with correct greeting', function () {
var callback = sinon.spy();
hello('foo', callback);
callback.should.have.been.calledWith('Hello foo');
});
});
};
}
);
Комментариев нет:
Отправить комментарий