Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Match anything that isn't undefined with .any #186

Closed
wants to merge 1 commit into from

5 participants

@ryangreenberg

This change modifies the jasmine.any argument matcher to allow it to match any object regardless of type. For example:

var fn = jasmine.createSpy();
fn("Chai", [1,2,3]);
expect(fn).toHaveBeenCalledWith(jasmine.any(String), jasmine.any()); // passes

var fn = jasmine.createSpy();
fn("Earl Grey");
expect(fn).toHaveBeenCalledWith(jasmine.any(String), jasmine.any()); // fails

I'd also be open to changing jasmine.any() (no args) to jasmine.anything(). I thought it was preferable not to introduce more terminology, but I see that .anything() may be more descriptive for readers than .any().

@ryangreenberg

Any reactions to this pull request? Preference for .anything vs. .any?

@JonRowe

+1 !

@infews
Owner

Does jasmine.any(Object) work in this case?

@slackersoft
Owner

jasmine.any(Object) will not match anything that's not an instance of object (Number, String, etc.)

We've made a tracker story for this here: https://www.pivotaltracker.com/story/show/58117878

If someone wants to rewrite/rebase this off of master and use a name other than .any (e.g. .something, .anything) we'd be happy to take a look at this again.

@slackersoft slackersoft closed this
@slackersoft slackersoft referenced this pull request from a commit
@slackersoft slackersoft Add `anything` matcher to match any value that is neither null or und…
…efined

[finish #58117878] Fix #186
482f4d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 16, 2012
  1. @ryangreenberg
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 0 deletions.
  1. +30 −0 spec/core/MatchersSpec.js
  2. +4 −0 src/core/Matchers.js
View
30 spec/core/MatchersSpec.js
@@ -982,6 +982,36 @@ describe("jasmine.Matchers", function() {
});
describe(".jasmineMatches", function () {
+ describe("with no argument", function() {
+ beforeEach(function () {
+ any = jasmine.any();
+ });
+
+ it("matches an empty object", function () {
+ expect(any.jasmineMatches({})).toEqual(true);
+ });
+
+ it("matches a newed up object", function () {
+ expect(any.jasmineMatches(new Object())).toEqual(true);
+ });
+
+ it("matches a string", function () {
+ expect(any.jasmineMatches("")).toEqual(true);
+ });
+
+ it("matches a number", function () {
+ expect(any.jasmineMatches(123)).toEqual(true);
+ });
+
+ it("matches a function", function () {
+ expect(any.jasmineMatches(function () {})).toEqual(true);
+ });
+
+ it("does not match undefined", function () {
+ expect(any.jasmineMatches()).toEqual(false);
+ });
+ });
+
describe("with Object", function () {
beforeEach(function () {
any = jasmine.any(Object);
View
4 src/core/Matchers.js
@@ -346,6 +346,10 @@ jasmine.Matchers.Any = function(expectedClass) {
};
jasmine.Matchers.Any.prototype.jasmineMatches = function(other) {
+ if (!this.expectedClass) {
+ return typeof other !== 'undefined';
+ }
+
if (this.expectedClass == String) {
return typeof other == 'string' || other instanceof String;
}
Something went wrong with that request. Please try again.