Logical Disjunction (OR) Using Mocha and Chai

I have the following scenario in which I have to verify that the URL is constructed correctly with some query arguments. I don’t expect the system to apply a specific order in the display URL, so I came up with the following test case, which I expected would work:

it('test that url is built correctly', function () {
    var args = {
        arg1: 'value1',
        arg1: 'value2'
    };

    var rendered_url = render_url(args);

    expect(rendered_url).to.equal('/my/url?arg1=value1&arg2=value2')
                     .or.to.equal('/my/url?arg2=value2&arg1=value1')
    ;
});

I was very surprised by the chain or, which was not there, as this makes the process of building the application clean and comfortable.

I know that I can get around this in different ways (for example, using satisfy), but I'm wondering:

  • I cannot find a template to achieve in a similar way what I want in the documentation (I read it completely) ...
  • ... or is there a good reason not to include this construct in chai ...
  • ... , , (, ..).
+4
2

to.include .match:

var chai = require("chai");
var expect = chai.expect;

var option1 = '/my/url?arg1=value1&arg2=value2';
var option2 = '/my/url?arg2=value2&arg1=value1';

var possible = [option1, option2];

var re = /^\/my\/url\?arg1=value1&arg2=value2|\/my\/url\?arg2=value2&arg1=value1$/;

it('1', function () {
    var rendered_url = option1;
    expect(possible).to.include(rendered_url);
    expect(rendered_url).to.match(re);
});

it('2', function () {
    var rendered_url = option2;
    expect(possible).to.include(rendered_url);
    expect(rendered_url).to.match(re);
});

it('3', function () {
    var rendered_url = "foo";
    expect(possible).to.include(rendered_url);
});

it('4', function () {
    var rendered_url = "foo";
    expect(rendered_url).to.match(re);
});

2 , 2 .

, possible, re , .

, , .or Chai, , Chai . , .equal, , . .or, .equal , . - expect(foo).to.equal(bar), equal , . - , promises, , , , : " ", expect(foo).to.equal(bar).end(). , , .

+5

- :

expect(rendrered_url).to.satisfy(function(url){
    return url === '/my/url?arg1=value1&arg2=value2 || url === '/my/url?arg2=value2&arg1=value1';
});
+2

Source: https://habr.com/ru/post/1569542/


All Articles