2

I'm using buster.js and the "expect" assertions. I want the assertion to succeed in at least one of:

expect(req.body.data._id).toBe('foo bar baz');

or

expect(req.body.data.id).toBe('foo bar baz');

How can I do it?

R01010010
  • 5,670
  • 11
  • 47
  • 77

3 Answers3

2

Having a look at the relevant documentation it looks like you want

expect([req.body.data._id, req.body.data.id]).toContain('foo bar baz');
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

While the answer from @glenn jackman is good enough, to get more semantic related reading, you could use Array.prototype.some function with some isEqual analog. Example:

var isEqual = function(first) { return function(second) { return first === second; }; }

[1,2,3].some(isEqual(2); // true

expect([1,2,3].some(isEqual(2))).toBeTrue(); // passed

expect([1,2,3].some(isEqual(4))).toBeTrue(); // failed

Or you could walk further on this way:

var someOf = function(array) { return array.some.bind(array); };

expect(someOf([1,2,3])(isEqual(1))).toBeTrue(); // passed

Get rid of brackets:

var someOf = function(array) {
    return { isEqual: function(x) { return array.some(isEqual(x)); } };
}

expect(someOf([1,2,3]).isEqual(4)).toBeTrue();

I believe you can do more with creating custom 'expectations' with buster.referee.add.

Microfed
  • 2,832
  • 22
  • 25
0

expect(req.body.data._id || req.body.data.id).toBe('foo bar baz');

R01010010
  • 5,670
  • 11
  • 47
  • 77
  • Are you sure this works? What if `req.body.data._id` is `'mango'` and `req.body.data.id` is `'foo bar baz'` – slebetman Feb 14 '16 at 21:41
  • Are you sure? Because that's not how javascript works. In the example I gave above, it basically becomes this `expect('mango' || 'foo bar baz').toBe('foo bar baz')` which boils down to `expect('mango').toBe('foo bar baz')`. That's what the `||` operator does - returns the **first** truthy value – slebetman Feb 15 '16 at 02:07
  • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. – Daniel Cheung Feb 15 '16 at 02:25