我首先想说的是,我是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