diff --git a/lib/sinon/spy.js b/lib/sinon/spy.js index 497dc3068..87ff6fcf5 100644 --- a/lib/sinon/spy.js +++ b/lib/sinon/spy.js @@ -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; @@ -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); }); }, diff --git a/test/issues/issues-test.js b/test/issues/issues-test.js index 90bf205ad..7e4007a1a 100644 --- a/test/issues/issues-test.js +++ b/test/issues/issues-test.js @@ -321,7 +321,7 @@ describe("issues", function () { }); }); - describe("#1512", function () { + describe("#1512 - sandbox.stub(obj,protoMethod)", function () { var sandbox; beforeEach(function () { @@ -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); + }); + + }); });