这是我第一次测试一个应用程序,这有点让人头痛。我建立了测试环境。我为我的测试文件夹中的茉莉index.html
看起来是这样的:茉莉花和Karma与骨干的问题
的index.html
<!doctype html>
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="stylesheet" href="../bower_components/jasmine/lib/jasmine-core/jasmine.css">
</head>
<body>
<script src="../bower_components/jasmine/lib/jasmine-core/jasmine.js"></script>
<script src="../bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script>
<script src="../bower_components/jasmine/lib/jasmine-core/boot.js"></script>
<!-- include source files here... -->
<script src="../public/js/main.js"></script>
<script src="../public/js/AppView.js"></script>
<script src="../public/js/FormLoanView.js"></script>
<script src="../public/js/FormLoanModel.js"></script>
<script src="../public/js/ResponseLoanModel.js"></script>
<script src="../public/js/ResultLoanView.js"></script>
<!-- include spec files here... -->
<script src="spec/test.js"></script>
</body>
</html>
test.js
(function() {
describe('Form Model', function() {
describe('when instantiated', function() {
it('should exhibit attributes', function() {
var formModel = new FormLoanModel({});
console.log(formModel)
expect(formModel.get("Annual Income"))
.toEqual("");
});
});
});
})();
打开当我index.html
我得到以下信息:
TypeError: undefined is not a function
所以它看起来像它运行我的测试。打开Chrome开发人员工具后,我得到以下内容:
Uncaught ReferenceError: Backbone is not defined
因此,我意识到jQuery和Backbone未被加载到测试中。我来了解到,Karma帮助我们实现了很多自动化。使用Yeoman建立业力后。我做编辑我karma.conf.js现在看起来是这样的:
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2015-07-12 using
// generator-karma 1.0.0
module.exports = function(config) {
'use strict';
config.set({
// enable/disable watching file and executing tests whenever any file changes
autoWatch: true,
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
// as well as any additional frameworks (requirejs/chai/sinon/...)
frameworks: [
"jasmine"
],
// list of files/patterns to load in the browser
files: [ "../lib/*.js","../public/js/*.js","./spec/*.js"
],
// list of files/patterns to exclude
exclude: [
],
// web server port
port: 8080,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: [
"Chrome"
],
// Which plugins to enable
plugins: [
"karma-phantomjs-launcher",
"karma-jasmine"
],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false,
colors: true,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};
我添加的文件是库,我的骨干模块,和我的茉莉花测试。打字karma start
后,我得到了以下成功的屏幕在终端指定的本地服务器:
Karma v0.12.37 - connected
Chrome 43.0.2357 (Mac OS X 10.10.2) is idle
所以最后在这一点上,我会刷新正常运行我的测试后,希望index.html
,但事实并非如此。它仍然警告我缺乏骨干和jQuery知识。任何人都可以帮我弄清楚我要去哪里错了吗?
文件Strucutre
ROOT
-----lib
--------------backbone.js
--------------underscore.js
--------------jquery-1.11.3.js
-----public
--------------js
---------------------*backbone modules*
-----test
--------------spec
----------------------test.js
--------------index.html
--------------karma.conf.js
backbone.js位于何处?您是否尝试明确地将文件路径写入karma.config文件的文件数组中?也许你的glob模式关闭了,它们不匹配必要的文件,所以它们不包括在内。 – doldt
所以我正在按照正确的过程?这是路径问题吗? – theamateurdataanalyst
我猜它**实际上是一个路径问题。骨干的js文件位于文件系统的哪里? – doldt