One of strings in array to match an expression

后端 未结 5 1597
花落未央
花落未央 2021-02-12 22:29

The Problem:

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 matc

5条回答
  •  逝去的感伤
    2021-02-12 22:48

    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/);
    

    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
              };
            }
          };
        }
      });
    });
    

提交回复
热议问题