這是我第一次測試一個應用程序,這有點讓人頭痛。我建立了測試環境。我爲我的測試文件夾中的茉莉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