我首先想說的是,我是RequireJS的新手,甚至是Jasmine的新手。獲取requirejs與Jasmine一起工作
我遇到了一些與SpecRunner有關的問題,需要JS。我一直在關注Uzi Kilon和Ben Nadel的教程(以及其他一些),他們幫助了一些人,但我仍然有一些問題。
看來,如果在測試中出現錯誤(我可以想到一個特別的類型錯誤),spec runner html會顯示。這告訴我,我在JavaScript中有一些問題。但是,在我解決這些錯誤之後,不再顯示HTML。 我無法讓測試運行器顯示。有人會發現我的代碼有問題會導致此問題?
這裏是我的目錄結構:
Root
|-> lib
|-> jasmine
|-> lib (contains all of the jasmine lib)
|-> spec
|-> src
|-> jquery (jquery js file)
|-> require (require js file)
index.html (spec runner) specRunner.js
這裏是SpecRunner(指數)HTML:
<!doctype html>
<html lang="en">
<head>
<title>Javascript Tests</title>
<link rel="stylesheet" href="lib/jasmine/lib/jasmine.css">
<script src="lib/jasmine/lib/jasmine.js"></script>
<script src="lib/jasmine/lib/jasmine-html.js"></script>
<script src="lib/jquery/jquery.js"></script>
<script data-main="specRunner" src="lib/require/require.js"></script>
<script>
require({ paths: { spec: "lib/jasmine/spec" } }, [
// Pull in all your modules containing unit tests here.
"spec/notepadSpec"
], function() {
jasmine.getEnv().addReporter(new jasmine.HtmlReporter());
jasmine.getEnv().execute();
});
</script>
</head>
<body>
</body>
</html>
這裏是specRunner.js(配置)
require.config({
urlArgs: 'cb=' + Math.random(),
paths: {
jquery: 'lib/jquery',
jasmine: 'lib/jasmine/lib/jasmine',
'jasmine-html': 'lib/jasmine/lib/jasmine-html',
spec: 'lib/jasmine/spec/'
},
shim: {
jasmine: {
exports: 'jasmine'
},
'jasmine-html': {
deps: ['jasmine'],
exports: 'jasmine'
}
}
});
這裏有一個規範:
require(["../lib/jasmine/src/notepad"], function (notepad) {
describe("returns titles", function() {
expect(notepad.noteTitles()).toEqual("");
});
});
記事本來源:
define(['lib/jasmine/src/note'], function (note) {
var notes = [
new note('pick up the kids', 'dont forget to pick up the kids'),
new note('get milk', 'we need two gallons of milk')
];
return {
noteTitles: function() {
var val;
for (var i = 0, ii = notes.length; i < ii; i++) {
//alert(notes[i].title);
val += notes[i].title + ' ';
}
return val;
}
};
});
和NOTE源(JIC):
define(function(){
var note = function(title, content) {
this.title = title;
this.content = content;
};
return note;
});
我已經確定的是,就應用程序而言,路徑是正確的。一旦我得到這個工作,我可以玩弄配置這些路徑,以便它不那麼難過。
你能嘗試這個?在需求之外定義HtmlReported。只調用裏面執行。 var jasmineEnv = jasmine.getEnv(); jasmineEnv.addReporter(new jasmine.HtmlReporter()); require(['suites/aSpec.js'],function(spec){jsmineEnv.execute(); }); – basos 2013-05-11 11:39:20
對於茉莉花2.0.0獨立,這個答案適合我: http://stackoverflow.com/questions/19240302/does-jasmine-2-0-really-not-work-with-require-js/20851265#20851265 – shaunsantacruz 2014-02-19 19:16:38