数组中用于匹配表达式的字符串之一 [英] One of strings in array to match an expression

查看:147
本文介绍了数组中用于匹配表达式的字符串之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我有一个promises数组,它被解析为字符串数组。现在,如果至少有一个字符串与正则表达式匹配,则测试应该通过。

I have an array of promises which is resolved to an array of strings. Now the test should pass if at least one of the strings matches a regular expression.

目前,我使用简单的字符串连接解决它:

Currently, I solve it using simple string concatenation:

protractor.promise.all([text1, text2, text3]).then(function (values) {
    expect(values[0] + values[1] + values[2]).toMatch(/expression/);
});

显然,这个不能很好地扩展并且不是特别易读。

Obviously, this does not scale well and is not particularly readable.

问题:

可以使用解决问题自定义茉莉花匹配器,或 jasmine.any() 自定义非对称等式测试程序

推荐答案

您只需使用 map 获取 boolean 的列表,然后用断言结果toContain(true)

You could simply use map to get a list of boolean and then assert the result with toContain(true):

var all = protractor.promise.all;
var map = protractor.promise.map;

expect(map(all([text1, text2, text3]), RegExp.prototype.test, /expression/)).toContain(true);

您还可以使用自定义匹配器:

You could also use a custom matcher:

expect(all([text1, text2, text3])).toContainPattern(/expression/);

自定义匹配器在 beforeEach 中声明:

And the custom matcher declared in beforeEach:

beforeEach(function() {
  jasmine.addMatchers({
    toContainPattern: function() {
      return {
        compare: function(actual, regex) {
          return {
            pass: actual.some(RegExp.prototype.test, regex), 
            message: "Expected [" + actual + "] to have one or more match with " + regex
          };
        }
      };
    }
  });
});

这篇关于数组中用于匹配表达式的字符串之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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