有没有办法在自定义匹配器中使用

有没有办法在自定义匹配器中使用

本文介绍了有没有办法在自定义匹配器中使用Jasmine默认匹配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些形式的Jasmine测试规范中有一个自定义匹配器:

I have a custom matcher in some Jasmine test specs of the form:

this.addMatchers({
    checkContains: function(elem){
        var found = false;
        $.each( this.actual, function( actualItem ){

            // Check if these objects contain the same properties.
            found = found || actualItem.thing == elem;
        });
        return found;
    }
});

当然, actualItem.thing == elem 实际上并没有比较对象内容 - 我必须使用。

Of course, actualItem.thing == elem doesn't actually compare object contents- I have to use one of the more complex solutions in Object comparison in JavaScript.

我不禁注意到,Jasmine已经有了一个很好的对象相等检查器:。有没有办法在自定义匹配器中使用它?有没有在自定义匹配器中使用匹配器的一般方法?

I can't help but notice, though, that Jasmine already has a nice object equality checker: expect(x).toEqual(y). Is there any way to use that within a custom matcher? Is there any general way to use matchers within custom matchers?

推荐答案

是的,它有点hacky但完全可能。

Yes, it is slightly hacky but entirely possible.

我们需要做的第一件事是使Jasmine.Env类可用。就个人而言,我已经在我的SpecRunner.html中完成了这项工作,因为它已经在那里设置了。在我的SpecRunner的加载中,我运行了以下脚本:

The first thing we need to do is make the Jasmine.Env class available. Personally I have done this in my SpecRunner.html since its already setup there anyway. On the load of my SpecRunner I have the following script that runs:

(function() {
      var jasmineEnv = jasmine.getEnv();
      jasmineEnv.updateInterval = 1000;

      var trivialReporter = new jasmine.TrivialReporter();

      jasmineEnv.addReporter(trivialReporter);

      jasmineEnv.specFilter = function(spec) {
        return trivialReporter.specFilter(spec);
      };

      var currentWindowOnload = window.onload;

      window.onload = function() {
        if (currentWindowOnload) {
          currentWindowOnload();
        }
        execJasmine();
      };

      function execJasmine() {
        jasmineEnv.execute();
      };

    })();

所以在 execJasmine 函数声明之后我将jasmineEnv推送到全局命名空间添加这个:

So after the execJasmine function declaration I push the jasmineEnv into the global namespace by adding this:

this.jasmineEnv = jasmineEnv;

现在,在我的任何规范文件中,我都可以访问jasmineEnv变量,这是包含匹配器的内容核心代码。

Now, in any of my spec files I can access the jasmineEnv variable and that is what contains the matchers core code.

具体看toEqual,toEqual调用 jasmine.Env.prototype.equals _ 函数。这意味着您可以在customMatcher中执行以下操作:

Looking at toEqual specifically, toEqual calls the jasmine.Env.prototype.equals_ function. This means that in your customMatcher you can do the following:

beforeEach(function(){
    this.addMatchers({
        isJasmineAwesome    : function(expected){
            return jasmineEnv.equals_(this.actual, expected);
        }
    });
});

不幸的是,使用此方法只能访问以下方法:

Unfortunately, using this method will only give you access to the following methods:


  1. compareObjects _

  2. equals_

  3. 包含_

其余的匹配者都是jasmine.Matchers类,但我还没有公开。我希望这可以帮助你以某种方式或其他方式

The rest of the matchers reside the jasmine.Matchers class but I have not been able to make that public yet. I hope this helps you out in someway or another

这篇关于有没有办法在自定义匹配器中使用Jasmine默认匹配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:46