问题描述
有很多文档显示如何将匹配器添加到Jasmine规范中(,例如)。
There are plenty of documents that show how to add a matcher to a Jasmine spec (here, for example).
有没有人找到一种方法将匹配器添加到整个环境中;我想要创建一组有用的匹配器,可以通过任何和所有测试调用,而不是我的规范中的copypasta。
Has anyone found a way to add matchers to the whole environment; I'm wanting to create a set of useful matchers to be called by any and all tests, without copypasta all over my specs.
目前正在努力对源进行逆向工程,但是如果存在的话,我会更喜欢一个经过验证的方法。
Currently working to reverse engineer the source, but would prefer a tried and true method, if one exists.
推荐答案
当然,你只需要在每个人之前调用 ()
没有任何规范范围,并在那里添加匹配器。
Sure, you just call beforeEach()
without any spec scoping at all, and add matchers there.
这将全局添加 toBeOfType
matcher。
This would globally add a toBeOfType
matcher.
beforeEach(function() {
var matchers = {
toBeOfType: function(typeString) {
return typeof this.actual == typeString;
}
};
this.addMatchers(matchers);
});
describe('Thing', function() {
// matchers available here.
});
我创建了一个名为 spec_helper.js的文件
在我运行规范套件的其余部分之前,我需要加载到页面上的自定义匹配器之类的东西。
I've made a file named spec_helper.js
full of things like custom matchers that I just need to load onto the page before I run the rest of the spec suite.
这篇关于有没有办法在整个环境中添加Jasmine匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!