首先,我想说我对RequireJS很新,对Jasmine更是新手。
我在SpecRunner和require JS方面遇到了一些问题。我一直在遵循Uzi Kilon和Ben Nadel(以及其他一些人)的教程,他们提供了一些帮助,但我仍然有一些问题。
似乎如果测试中抛出错误(我可以想到一个类型错误),规范运行器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;
}
};
});
并且注释来源(JIC):
define(function (){
var note = function(title, content) {
this.title = title;
this.content = content;
};
return note;
});
我已经确保在应用程序方面路径是正确的。一旦我让它工作起来,我就可以尝试配置这些路径,使其不那么混乱。