Dar*_*rin 29 requirejs jasmine
我正在设置我的SpecRunner.html/.js,RequireConfig.js,我的路径和垫片,就像我早期发布的Jasmine + RequireJs候选版本一样,但现在我的测试方法显示Jasmine未定义.他们最近改用了另一种加载Jasmine的方法,据我所知,它与RequireJs不兼容.
我的理解是否正确?如果是这样,我们能再次使用Jasmine + RequireJs吗?
Eri*_*uth 69
新的boot.js做了一堆初始化并将它附加到window.onload(),它已被require.js加载Jasmine时调用.您可以手动调用window.onload()来初始化HTML Reporter并执行环境.
SpecRunner.html
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jasmine Spec Runner v2.0.0</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">
<!-- specRunner.js runs all of the tests -->
<script data-main="specRunner" src="../bower_components/requirejs/require.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
specRunner.js
(function() {
'use strict';
// Configure RequireJS to shim Jasmine
require.config({
baseUrl: '..',
paths: {
'jasmine': 'tests/lib/jasmine-2.0.0/jasmine',
'jasmine-html': 'tests/lib/jasmine-2.0.0/jasmine-html',
'boot': 'tests/lib/jasmine-2.0.0/boot'
},
shim: {
'jasmine': {
exports: 'window.jasmineRequire'
},
'jasmine-html': {
deps: ['jasmine'],
exports: 'window.jasmineRequire'
},
'boot': {
deps: ['jasmine', 'jasmine-html'],
exports: 'window.jasmineRequire'
}
}
});
// Define all of your specs here. These are RequireJS modules.
var specs = [
'tests/spec/routerSpec'
];
// Load Jasmine - This will still create all of the normal Jasmine browser globals unless `boot.js` is re-written to use the
// AMD or UMD specs. `boot.js` will do a bunch of configuration and attach it's initializers to `window.onload()`. Because
// we are using RequireJS `window.onload()` has already been triggered so we have to manually call it again. This will
// initialize the HTML Reporter and execute the environment.
require(['boot'], function () {
// Load the specs
require(specs, function () {
// Initialize the HTML Reporter and execute the environment (setup by `boot.js`)
window.onload();
});
});
})();
Run Code Online (Sandbox Code Playgroud)
示例规范
define(['router'], function(router) {
'use strict';
describe('router', function() {
it('should have routes defined', function() {
router.config({});
expect(router.routes).toBeTruthy();
});
});
});
Run Code Online (Sandbox Code Playgroud)
cur*_*ran 13
这是另一种在某些情况下可能更简单的方法 - 在执行测试之前使用Jasmine的异步支持来加载AMD模块,如下所示:
在MySpec.js中:
describe('A suite', function() {
var myModule;
// Use require.js to fetch the module
it("should load the AMD module", function(done) {
require(['myModule'], function (loadedModule) {
myModule = loadedModule;
done();
});
});
//run tests that use the myModule object
it("can access the AMD module", function() {
expect(myModule.speak()).toBe("hello");
});
});
Run Code Online (Sandbox Code Playgroud)
为此,您需要在SpecRunner.html中包含require.js,并可能配置require(通常情况下,例如通过设置baseUrl),如下所示:
在SpecRunner.html中:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Jasmine Spec Runner v2.0.0</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">
<script src="lib/require.min.js"></script>
<script> require.config({ baseUrl: "src" }); </script>
<script src="lib/jasmine-2.0.0/jasmine.js"></script>
<script src="lib/jasmine-2.0.0/jasmine-html.js"></script>
<script src="lib/jasmine-2.0.0/boot.js"></script>
<script src="spec/MySpec.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
对于此示例,AMD模块实现可能如下所示:
在src/myModule.js中:
define([], function () {
return {
speak: function () {
return "hello";
}
};
});
Run Code Online (Sandbox Code Playgroud)
这是一个工作的Plunk,它实现了这个完整的例子.
请享用!
归档时间: |
|
查看次数: |
9805 次 |
最近记录: |