Backbone.Deferred.Model:为什么单元测试不是运行?
Backbone.Deferred.Model: Why the unittest is not run?
这是有问题的代码。
define (require) ->
Backbone = require 'backbone'
require 'backbone-deferred'
class Data extends Backbone.Deferred.Model
urlRoot: 'data'
parse: (resp) ->
resp.record or resp
isValid: ->
@get 'valid'
# Newly introduced
dataUrl: ->
@get('data_url')?.replace /abc/, ''
这是单元测试
define (require) ->
Data = require 'model/Data'
describe 'dataurl', ->
describe 'No change', ->
data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png'
expect(data.dataUrl()).to.equal('https://m/a.png')
出于某种原因,测试不是 运行 mocha。
我正在用这个html来驱动测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="../bower_components/mocha/mocha.css" type="text/css" media="screen" title="Tests for {%= name %}" charset="utf-8">
</head>
<body>
<div id="mocha"></div>
<script src="../bower_components/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
<script src="../bower_components/chai/chai.js" type="text/javascript" charset="utf-8"></script>
<script src="../bower_components/requirejs/require.js" type="text/javascript" charset="utf-8"></script>
<!-- Requirejs configuration -->
<script src="../temp/local/scripts/config.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
// Dumping console messages to /dev/null to avoid parsing error
// when grunt:mocha_phantomjs is run
if (navigator.userAgent.indexOf('PhantomJS') >= 0) {
var log = console.log
console.log = function(message) {
if (message.indexOf('<') === 0) {
log.apply(this, Array.prototype.slice.call(arguments))
}
}
}
// Mixpanel is not loaded for unit testing.
// Redirecting mixpanel to /dev/null.
window.mixpanel = {
track: function() {}
};
require.config({
baseUrl: "../temp/local/scripts",
shim: {
'chai-jquery': {
deps: ['jquery']
},
'sinon': {
exports: 'sinon'
}
},
paths: {
// Aliasing path to find the specs
'specs': '../../specs',
'sinon' : '../../../bower_components/sinon/lib/sinon',
'sinon-chai' : '../../../bower_components/sinon-chai/lib/sinon-chai',
'chai-jquery': '../../../bower_components/chai-jquery/chai-jquery',
'handlebars' : '../../../bower_components/handlebars/handlebars',
}
});
runTests = function() {
// Ensuring that jquery and chai-jquery are loaded
require(
['jquery', 'chai-jquery', 'sinon-chai', 'handlebars'],
function($, jqueryChai, sinonChai, Handlebars) {
// Registering testing style in globals for easy access
window.expect = chai.expect;
chai.use(jqueryChai);
chai.use(sinonChai)
mocha.setup({
ui: 'bdd',
ignoreLeaks: true
});
// Protect from barfs
console = window.console || function() {};
// Don't track
window.notrack = true;
// Helper... not really needed but in case we want to do something fancy
var runMocha = function() {
mocha.run();
};
require([
// Many other tests; removed for clarity
'specs/data.spec'
], function() {
// For xUnit and code coverage reports, we run the tests
// using mocha-phantomjs runner.
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
}
</script>
</body>
</html>
这里是 g运行t 测试任务的输出
Running "mocha:default" (mocha) task
Testing: test/index.html
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․
1081 passing (9s)
>> 1081 passed! (9.17s)
我的问题是:
1) 添加新测试后,测试数量完全改变。新试卷未被采纳的可能原因有哪些?
2) 如果我故意在我的单元测试中引入一个错误,它会在测试失败的情况下导致:
Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue.
我确定这与测试有关,但没有足够的信息来诊断问题。如何打开日志记录或获取其他信息?
您的测试用例应该使用 it
函数而不是 describe
(用于对测试进行分组)来定义
define (require) ->
Data = require 'model/Data'
describe 'dataurl', ->
it 'No change', ->
data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png'
expect(data.dataUrl()).to.equal('https://m/a.png')
我已经创建了 this jdfiddle,因此您可以看到与原始定义的区别。
如果在测试中引入错误,如果测试未在 it
函数中定义,那么它将影响整个测试 运行。你可以在这个other fiddle中查看(你会注意到第二个测试在错误定义的测试中出错后没有运行)
这是有问题的代码。
define (require) ->
Backbone = require 'backbone'
require 'backbone-deferred'
class Data extends Backbone.Deferred.Model
urlRoot: 'data'
parse: (resp) ->
resp.record or resp
isValid: ->
@get 'valid'
# Newly introduced
dataUrl: ->
@get('data_url')?.replace /abc/, ''
这是单元测试
define (require) ->
Data = require 'model/Data'
describe 'dataurl', ->
describe 'No change', ->
data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png'
expect(data.dataUrl()).to.equal('https://m/a.png')
出于某种原因,测试不是 运行 mocha。
我正在用这个html来驱动测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="../bower_components/mocha/mocha.css" type="text/css" media="screen" title="Tests for {%= name %}" charset="utf-8">
</head>
<body>
<div id="mocha"></div>
<script src="../bower_components/mocha/mocha.js" type="text/javascript" charset="utf-8"></script>
<script src="../bower_components/chai/chai.js" type="text/javascript" charset="utf-8"></script>
<script src="../bower_components/requirejs/require.js" type="text/javascript" charset="utf-8"></script>
<!-- Requirejs configuration -->
<script src="../temp/local/scripts/config.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
// Dumping console messages to /dev/null to avoid parsing error
// when grunt:mocha_phantomjs is run
if (navigator.userAgent.indexOf('PhantomJS') >= 0) {
var log = console.log
console.log = function(message) {
if (message.indexOf('<') === 0) {
log.apply(this, Array.prototype.slice.call(arguments))
}
}
}
// Mixpanel is not loaded for unit testing.
// Redirecting mixpanel to /dev/null.
window.mixpanel = {
track: function() {}
};
require.config({
baseUrl: "../temp/local/scripts",
shim: {
'chai-jquery': {
deps: ['jquery']
},
'sinon': {
exports: 'sinon'
}
},
paths: {
// Aliasing path to find the specs
'specs': '../../specs',
'sinon' : '../../../bower_components/sinon/lib/sinon',
'sinon-chai' : '../../../bower_components/sinon-chai/lib/sinon-chai',
'chai-jquery': '../../../bower_components/chai-jquery/chai-jquery',
'handlebars' : '../../../bower_components/handlebars/handlebars',
}
});
runTests = function() {
// Ensuring that jquery and chai-jquery are loaded
require(
['jquery', 'chai-jquery', 'sinon-chai', 'handlebars'],
function($, jqueryChai, sinonChai, Handlebars) {
// Registering testing style in globals for easy access
window.expect = chai.expect;
chai.use(jqueryChai);
chai.use(sinonChai)
mocha.setup({
ui: 'bdd',
ignoreLeaks: true
});
// Protect from barfs
console = window.console || function() {};
// Don't track
window.notrack = true;
// Helper... not really needed but in case we want to do something fancy
var runMocha = function() {
mocha.run();
};
require([
// Many other tests; removed for clarity
'specs/data.spec'
], function() {
// For xUnit and code coverage reports, we run the tests
// using mocha-phantomjs runner.
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
}
else {
mocha.run();
}
});
});
}
</script>
</body>
</html>
这里是 g运行t 测试任务的输出
Running "mocha:default" (mocha) task
Testing: test/index.html
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
․․․․․․․․․․․
1081 passing (9s)
>> 1081 passed! (9.17s)
我的问题是:
1) 添加新测试后,测试数量完全改变。新试卷未被采纳的可能原因有哪些?
2) 如果我故意在我的单元测试中引入一个错误,它会在测试失败的情况下导致:
Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue.
我确定这与测试有关,但没有足够的信息来诊断问题。如何打开日志记录或获取其他信息?
您的测试用例应该使用 it
函数而不是 describe
(用于对测试进行分组)来定义
define (require) ->
Data = require 'model/Data'
describe 'dataurl', ->
it 'No change', ->
data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png'
expect(data.dataUrl()).to.equal('https://m/a.png')
我已经创建了 this jdfiddle,因此您可以看到与原始定义的区别。
如果在测试中引入错误,如果测试未在 it
函数中定义,那么它将影响整个测试 运行。你可以在这个other fiddle中查看(你会注意到第二个测试在错误定义的测试中出错后没有运行)