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

查看:141
本文介绍了有没有办法在整个环境中添加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.

推荐答案

当然,你只需要在每个人之前调用 ()没有任何规范范围,并在那里添加匹配器。

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

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