gpt4 book ai didi

unit-testing - 带有 Jasmine 和 RequireJS 的 Squire.js 示例

转载 作者:行者123 更新时间:2023-12-03 12:15:24 26 4
gpt4 key购买 nike

我想写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 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

在您提出问题时,Jasmine 1.3 是最新版本。从那时起 2.0 发布并包含一些异步改进。两个版本都包含在这里,1.3 被注释掉了。

SpecRunner.html:
<!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>

SpecRunner.js:

这个问题 Does Jasmine 2.0 really not work with require.js?有助于启动和运行它。
(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
})();

foo.js:
define(['knockout'], function (ko) {

var message = ko.observable("Go away, World!");

return {
message: message()
};
});

你好.js:
define(['foo'], function (foo) {

return {
message : foo.message
};

});

你好-spec.js:
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!"
});

模拟 hello 对 foo 的依赖,然后
builder.require(['hello'], function (hi) {
hello = hi;
done(); // ...until you invoke it.
});

使用模拟的 foo 加载 hello 模块。与在第二个测试中使用 require 本身加载 hello 相比:
 require(['hello'], function (hi) {
hello = hi;
done(); // ...until you invoke it.
});

Jasmine 文档 http://jasmine.github.io/可以填写“done()”功能(2.0)或“runs/waitsFor”(1.3)。

关于unit-testing - 带有 Jasmine 和 RequireJS 的 Squire.js 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19089472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com