Skip to content

Commit

Permalink
Cache reference to filter in stubbing calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Aug 10, 2017
1 parent 3df68a7 commit d8a2fc0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/sinon/spy.js
Expand Up @@ -12,11 +12,14 @@ var wrapMethod = require("./util/core/wrap-method");
var sinonFormat = require("./util/core/format");
var valueToString = require("./util/core/value-to-string");

/* cache references to library methods so that they also can be stubbed without problems */
var push = Array.prototype.push;
var slice = Array.prototype.slice;
var callId = 0;
var filter = Array.prototype.filter;
var ErrorConstructor = Error.prototype.constructor;

var callId = 0;

function spy(object, property, types) {
var descriptor, methodDesc;

Expand Down Expand Up @@ -340,7 +343,7 @@ var spyApi = {
},

matchingFakes: function (args, strict) {
return (this.fakes || []).filter(function (fake) {
return filter.call(this.fakes || [], function (fake) {
return fake.matches(args, strict);
});
},
Expand Down
21 changes: 20 additions & 1 deletion test/issues/issues-test.js
Expand Up @@ -321,7 +321,7 @@ describe("issues", function () {
});
});

describe("#1512", function () {
describe("#1512 - sandbox.stub(obj,protoMethod)", function () {
var sandbox;

beforeEach(function () {
Expand All @@ -341,4 +341,23 @@ describe("issues", function () {
assert(stub.called);
});
});

describe("#1521 - stubbing Array.prototype.filter", function() {
var orgFilter;

before(function () {
orgFilter = Array.prototype.filter;
});

afterEach(function () {
Array.prototype.filter = orgFilter;
});

it("should be possible stub filter", function() {
var stub = sinon.stub(Array.prototype, "filter");
[1,2,3].filter(function() { return false; });
assert(stub.calledOnce);
});

});
});

0 comments on commit d8a2fc0

Please sign in to comment.