有没有办法在整个环境中添加 Jasmine 匹配器 [英] Is there a way to add a Jasmine matcher to the whole environment

查看:25
本文介绍了有没有办法在整个环境中添加 Jasmine 匹配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多文档展示了如何向 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.

推荐答案

当然,您只需调用 beforeEach() 根本没有任何规范作用域,并在那里添加匹配器.

Sure, you just call beforeEach() without any spec scoping at all, and add matchers there.

这将全局添加一个 toBeOfType 匹配器.

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 匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆