- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想写JS测试。生产代码是用 RequireJS 编写的。
我找到了一个名为 Squire.js 的测试库:https://github.com/iammerrick/Squire.js/
来自 Squire.js 网站
Run generates a function that will receive a done callback and execute it after your test function is complete. Particularly useful for frameworks where asynchrony is handled with a callback. Here is an example with Mocha.js. Jasmine can offer this callback approach using Jasmin.Async."
最佳答案
这是一个很好的设置,可以将具有模拟依赖项的模块插入到测试中。这是一个让某人开始的端到端示例,如果您只想查看规范,请跳到最后。
文件夹结构:
Jasmine
|-lib
||-jasmine -- Contains all the Jasmine files
|||-boot.js
|||-jasmine-html.js
|||-jasmine.js
|||-jasmine.css
|||-jasmine_favicon.png
|||-...
|-spec -- Spec files go here
||-hello-spec.js
|SpecRunner.html
|SpecRunner.js
|squire.js
Scripts
|knockout.js
|require.js
|jquery.js
App
|-hello.js
|-foo.js
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine/jasmine.css">
</head>
<body>
<!-- Load RequireJS & the testsuite -->
<script src="../Scripts/require.js" data-main="SpecRunner.js" type="text/javascript" ></script>
</body>
</html>
(function () {
'use strict';
// Configure RequireJS to shim Jasmine
requirejs.config({
baseUrl: "../App",
paths: {
'jasmine' : '../Jasmine/lib/jasmine/jasmine',
'jasmine-html': '../Jasmine/lib/jasmine/jasmine-html',
'boot': '../Jasmine/lib/jasmine/boot', // This is not present in Jasmine 1.3
'spec' : '../Jasmine/spec',
'squire': '../Jasmine/squire',
'knockout': '../Scripts/knockout-2.3.0',
'jquery': '../Scripts/jquery-1.10.2' // This only used in the Jasmine 1.3 case.
},
shim: {
'jasmine': {
exports: 'jasmine'
},
'jasmine-html': {
deps: ['jasmine'],
exports: 'jasmine'
},
'boot': {
deps: ['jasmine', 'jasmine-html'],
exports: 'jasmine'
},
"squire": {
exports: "squire"
}
}
});
// Define all of your specs here. These are RequireJS modules.
var specs = [
'spec/hello-spec'
];
// 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();
});
});
/******
* Use this require if you're on Jasmine 1.3
******/
//require(['jquery', 'jasmine-html'], function ($, jasmine) {
// var jasmineEnv = jasmine.getEnv();
// jasmineEnv.updateInterval = 1000;
// var htmlReporter = new jasmine.HtmlReporter();
// jasmineEnv.addReporter(htmlReporter);
// jasmineEnv.specFilter = function(spec) {
// return htmlReporter.specFilter(spec);
// };
// $(function() {
// require(specs, function(spec) {
// jasmineEnv.execute();
// });
// });
//});
// end 1.3
})();
define(['knockout'], function (ko) {
var message = ko.observable("Go away, World!");
return {
message: message()
};
});
define(['foo'], function (foo) {
return {
message : foo.message
};
});
define(['squire'], function (Squire) {
var injector = new Squire();
var builder = injector
.mock('foo', {
message: "Hello, World!"
});
describe('hello', function () {
var hello;
beforeEach(function (done) {
// For async:
// accept a "done" parameter and Jasmine will wait...
builder.require(['hello'], function (hi) {
hello = hi;
done(); // ...until you invoke it.
});
/*******
* Use the following if you're on 1.3
*********/
//var done;
//runs(function () {
// builder.require(['hello'], function (hi) {
// hello = hi;
// done = true;
// });
//});
//waitsFor(function () {
// return done;
//}, "Unable to load dependency not loaded", 750);
// end 1.3
});
it('is welcoming', function() {
expect(hello.message).toBe("Hello, World!");
});
});
describe('hello no mock foo', function () {
var hello;
beforeEach(function (done) {
// For async:
// accept a "done" parameter and Jasmine will wait...
require(['hello'], function (hi) {
hello = hi;
done(); // ...until you invoke it.
});
/*******
* Use the following if you're on 1.3
*********/
//var done;
//runs(function () {
// require(['hello'], function (hi) {
// hello = hi;
// done = true;
// });
//});
//waitsFor(function () {
// return done;
//}, "Unable to load dependency not loaded", 750);
// end 1.3
});
it('is less than welcoming', function () {
expect(hello.message).toBe("Go away, World!");
});
});
});
var injector = new Squire();
var builder = injector
.mock('foo', {
message: "Hello, World!"
});
builder.require(['hello'], function (hi) {
hello = hi;
done(); // ...until you invoke it.
});
require(['hello'], function (hi) {
hello = hi;
done(); // ...until you invoke it.
});
关于unit-testing - 带有 Jasmine 和 RequireJS 的 Squire.js 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19089472/
如何使用 requirejs 导入 recaptcha。我已经尝试了几件事,但没有任何效果。 我需要这样做,以便能够在加载后使用 reCaptcha 的渲染方法自行渲染它。 require.confi
我正在尝试将 OpenLayers 库与 RequireJS 一起使用。 问题是,OpenLayers 一直处于“未定义”状态,即使它被列为我的模块的唯一依赖项: define(['OpenLayer
我正在尝试使用 gulp-requirejs构建一个演示项目。我希望结果是一个包含所有 js 依赖项和模板的单个文件。这是我的 gulpfile.js var gulp = require('gulp
没有 Require.js 就可以在浏览器控制台中调用全局对象,例如 app app.movies 当我将代码包装在 RequireJS 模块中时,如何在浏览器控制台中访问模块中的数据?我会发现这有助
这个问题有 3 个部分。我有两个模块 a 和 b。 如何在最初需要 b 后重新定义它? 如果 a 依赖于 b,我该如何更新 a 以使用新的 b? 如何找到所有依赖于 b 的模块并更新它们? 例如;
我正在尝试找到一种将 Backbone-relational 子模型与 RequireJS 一起使用的方法,其中子模型与 super 模型位于不同的文件中。 例如: // app.js define(
使用 require.js (r.js) 优化 js 代码时,将所有 js 源代码复制到目标目录(dir 属性)。 有没有办法(一些配置)来防止 requirejs 复制源文件? 最佳答案 如果添加
main.js 文件中的代码如下: phantom.injectJs("libs/require-1.0.7.js"); require.config( {
我在 require-config.js 中配置了一些路径,如下所示: var require = { baseUrl: '/javascript', paths: {
我正在重构一个大型 javascript 代码库以使用 RequireJS。不幸的是,我正在处理的许多文件都不是面向对象的,并且在不进行重大修改的情况下无法返回对象。是否有更有效的方法让“依赖”模块访
我是 RequireJS 的新手,我对加载顺序感到困惑。 我有一个全局项目配置,需要在位于 js/app/* 的模块之前加载。 这是我的结构: index.html config.js js/
我已经使用 requireJS 设置了一个 Angular 应用程序。在部署之前,我将所有内容捆绑到一个唯一的文件中。通常,外部库没有 requireJS“定义”包装器。 例如Angular Para
假设我有一个如下所示的模块: define(['jquery', 'actions', 'util', 'text!../templates/dialog.html!strip', 'text!../
如何使用 grunt-contrib-requirejs配置或什至r.js Config不缩小特定文件。 我可以使用optimize: 'none'选项禁用所有文件的缩小。但我不知道如何对单个文件禁用
好吧,我已经知道你应该像这样使用 RequireJS 配置路径 require.config({ paths: { name: 'value' } }); 并这样调用它。 requir
我希望能够有选择地定义一个模块,然后在我的代码中使用或不使用它。我正在考虑的特殊情况是在调试/测试环境中加载模拟/ stub 模块,但不是在我上线时加载。这是示例: 在 html 文件中,可以选择加载
我为构建过程创建了一个任务,其中 requirejs 作为它的子任务之一,并且在 requirejs 之后几乎没有其他任务。该任务在运行 requirejs 后停止,即使使用详细信息也不会抛出错误。任
我想创建一个可以从我的 RequireJS 项目中的任何模块访问的变量。 例如,在初始化时我想设置: this.myVar = 123; 并且能够在我的 RequireJS 项目内部(但不是外部)的任
我正在使用 TypeScript、Backbone 和 Mustache 编写网络应用程序。我想使用 Requirejs 进行依赖加载。 我还在使用 TypeScript 的 Web Essentia
我正在尝试创建一个节点样板,并尝试创建一个任务来运行 Jasmine 测试。我的 Gruntfile.js 中有以下配置: jasmine: { src : ['static/test/spec/
我是一名优秀的程序员,十分优秀!