有点扼杀,这不是基本功能
您可以像这样添加自定义匹配器:
JasmineExtensions.js
yourGlobal.addExtraMatchers = function () {
var addMatcher = function (name, func) {
func.name = name;
jasmine.matchers[name] = func;
};
addMatcher("toBeGreaterThanOrEqualTo", function () {
return {
compare: function (actual, expected) {
return {
pass: actual >= expected
};
}
};
}
);
};
实际上,您正在为匹配器定义一个构造函数-它是一个返回匹配器对象的函数。
在“引导”之前将其包括在内。基本匹配器在引导时加载。
您的html文件应如下所示:
<!-- jasmine test framework-->
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
<!-- custom matchers -->
<script type="text/javascript" src="Tests/JasmineExtensions.js"></script>
<!-- initialisation-->
<script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>
然后在boot.js中添加调用,以在定义了jasmine之后但在jasmine.getEnv()之前添加匹配器。Get env实际上是一个(有点误导性的)设置调用。
匹配器在Env构造函数中对setupCoreMatchers的调用中获得设置。
/**
* ## Require & Instantiate
*
* Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
*/
window.jasmine = jasmineRequire.core(jasmineRequire);
yourGlobal.addExtraMatchers();
/**
* Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
*/
jasmineRequire.html(jasmine);
/**
* Create the Jasmine environment. This is used to run all specs in a project.
*/
var env = jasmine.getEnv();
它们显示了在样本测试中添加自定义匹配器的另一种方法,但是它的工作方式是在每个测试之前使用来重新创建匹配器beforeEach
。这似乎太可怕了,所以我想我会改用这种方法。