Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value be Something OR SomethingElse #138

Open
alanjames1987 opened this issue Jun 14, 2015 · 6 comments
Open

Value be Something OR SomethingElse #138

alanjames1987 opened this issue Jun 14, 2015 · 6 comments

Comments

@alanjames1987
Copy link

I'm trying to test that a value is one value or another. In either case it's considered passing but I have not been able to figure out how to test for this.

Does expect.js support this sort of assertion and if not how should I go about testing for it?

@alanjames1987
Copy link
Author

I know I can use a ternary to select the correct value before giving it to expect, but that seems sloppy to me if expect supports this already.

@janmarek
Copy link
Contributor

What about

expect(value === 1 || value === 'abcd').to.be(true);

?

@alanjames1987
Copy link
Author

In my tests I have currently written something like that, but the logical operator was in the .be(...) area.

expect(actualValue).to.be(testValue === 1 || testValue === 'abcd');

This works but it was ugly. I was looking for something better.

@panstav
Copy link

panstav commented Sep 20, 2015

I usually declare variables for each such option, especially when I have >= 3 of those. So for example you could write:

var first = testValue === 1;
var second = testValue === 'abcd';

expect(actual).to.be(first || second);

@ghost
Copy link

ghost commented Oct 16, 2015

 expect([1, 'abcd']).to.contain(actualValue)

@rafis
Copy link

rafis commented Dec 10, 2015

Another variant:

expect.Assertion.prototype.stringOrArrayOfStrings = function() {
    var ok;
    if (typeof this.obj == 'string') {
        ok = true;
    } else if (this.obj instanceof Array) {
        ok = true;
        for(var i = 0; i < this.obj.length; ++i) {
            if (typeof this.obj[i] != 'string') {
                ok = false;
                break;
            }
        }
    } else {
        ok = false;
    }
    this.assert(ok, function() {
        return 'expected ' + expect.stringify(this.obj) + ' to be a string or an array of strings';
    }, function() {
        return 'expected ' + expect.stringify(this.obj) + ' to not be a string or an array of strings';
    });
};
expect(config.session.secret).to.be.stringOrArrayOfStrings();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants