Skip to content

Commit

Permalink
Jasmine matchers toHaveBeenCalled and toHaveBeenCalledWith gracefully…
Browse files Browse the repository at this point in the history
… overridden.
  • Loading branch information
milichev committed Feb 28, 2013
1 parent 9b4e935 commit 0b51285
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/jasmine-sinon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"alwaysThrew": "toHaveAlwaysThrown"
},

getMatcherFunction = function(sinonName) {
return function() {
getMatcherFunction = function(sinonName, matcherName) {
var original = jasmine.Matchers.prototype[matcherName];
return function () {
if (jasmine.isSpy(this.actual) && original) {
return original.apply(this, arguments);
}
var sinonProperty = this.actual[sinonName];
return (typeof sinonProperty === 'function') ? sinonProperty.apply(this.actual, arguments) : sinonProperty;
};
Expand All @@ -21,11 +25,11 @@
var sinonName = spyMatchers[i],
matcherName = "toHaveBeen" + sinonName.charAt(0).toUpperCase() + sinonName.slice(1);

spyMatcherHash[matcherName] = getMatcherFunction(sinonName);
spyMatcherHash[matcherName] = getMatcherFunction(sinonName, matcherName);
};

for (var j in unusualMatchers) {
spyMatcherHash[unusualMatchers[j]] = getMatcherFunction(j);
spyMatcherHash[unusualMatchers[j]] = getMatcherFunction(j, unusualMatchers[j]);
}

global.sinonJasmine = {
Expand Down
39 changes: 36 additions & 3 deletions spec/jasmine-sinon-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe("jasmine-sinon", function() {
this.spy.call({});
expect(this.spy.alwaysCalledOn(this)).toBeFalsy();
expect(this.spy).not.toHaveBeenAlwaysCalledOn(this);
})
});

});

Expand Down Expand Up @@ -554,7 +554,7 @@ describe("jasmine-sinon", function() {
myMethod: function(val) {
this.methodVal = val;
}
}
};
this.spy = sinon.spy(this.api, "myMethod");
});

Expand Down Expand Up @@ -645,7 +645,7 @@ describe("jasmine-sinon", function() {
foo: function() {
return 'bar';
}
}
};
this.methodStub = sinon.stub(this.api,'foo');
});

Expand Down Expand Up @@ -694,4 +694,37 @@ describe("jasmine-sinon", function() {

});

describe("jasmine matchers gracefully overridden", function() {

beforeEach(function() {
this.methodVal = "no";
this.api = {
myMethod: function(val) {
this.methodVal = val;
}
};
spyOn(this.api, "myMethod").andCallThrough();
});

describe("toHaveBeenCalled", function() {
it("should work for jasmine spy", function() {
expect(this.methodVal).toEqual("no");
expect(this.api.myMethod).not.toHaveBeenCalled();
this.api.myMethod.call(this, "yes");
expect(this.methodVal).toEqual("yes");
expect(this.api.myMethod).toHaveBeenCalled();
});
});

describe("toHaveBeenCalledWith", function() {
it("should work for jasmine spy", function() {
expect(this.methodVal).toEqual("no");
expect(this.api.myMethod).not.toHaveBeenCalledWith("yes");
this.api.myMethod.call(this, "yes");
expect(this.methodVal).toEqual("yes");
expect(this.api.myMethod).toHaveBeenCalledWith("yes");
});
});

});
});

0 comments on commit 0b51285

Please sign in to comment.