diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 476bf535..fc1cc6f1 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2252,6 +2252,13 @@ getJasmineRequireObj().ArrayContaining = function(j$) { throw new Error('You must provide an array to arrayContaining, not ' + j$.pp(this.sample) + '.'); } + // If the actual parameter is not an array, we can fail immediately, since it couldn't + // possibly be an "array containing" anything. However, we also want an empty sample + // array to match anything, so we need to double-check we aren't in that case + if (!j$.isArray_(other) && this.sample.length > 0) { + return false; + } + for (var i = 0; i < this.sample.length; i++) { var item = this.sample[i]; if (!j$.matchersUtil.contains(other, item, customTesters)) { diff --git a/spec/core/asymmetric_equality/ArrayContainingSpec.js b/spec/core/asymmetric_equality/ArrayContainingSpec.js index 778eca43..30b5aaad 100644 --- a/spec/core/asymmetric_equality/ArrayContainingSpec.js +++ b/spec/core/asymmetric_equality/ArrayContainingSpec.js @@ -31,6 +31,12 @@ describe("ArrayContaining", function() { expect(containing.asymmetricMatch(["bar"])).toBe(false); }); + it("does not match when the actual is not an array", function() { + var containing = new jasmineUnderTest.ArrayContaining(["foo"]); + + expect(containing.asymmetricMatch("foo")).toBe(false); + }); + it("jasmineToStrings itself", function() { var containing = new jasmineUnderTest.ArrayContaining([]); diff --git a/src/core/asymmetric_equality/ArrayContaining.js b/src/core/asymmetric_equality/ArrayContaining.js index b39fb857..92a85ed0 100644 --- a/src/core/asymmetric_equality/ArrayContaining.js +++ b/src/core/asymmetric_equality/ArrayContaining.js @@ -8,6 +8,13 @@ getJasmineRequireObj().ArrayContaining = function(j$) { throw new Error('You must provide an array to arrayContaining, not ' + j$.pp(this.sample) + '.'); } + // If the actual parameter is not an array, we can fail immediately, since it couldn't + // possibly be an "array containing" anything. However, we also want an empty sample + // array to match anything, so we need to double-check we aren't in that case + if (!j$.isArray_(other) && this.sample.length > 0) { + return false; + } + for (var i = 0; i < this.sample.length; i++) { var item = this.sample[i]; if (!j$.matchersUtil.contains(other, item, customTesters)) {