Spying attempt (Jasmine) in Array.prototype methods causes stack overflow

This is pretty weird. Using a runner testemwith jasmine2, the following specification is executed (although it correctly indicates that there are no expectations):

describe('Spying on array.prototype methods', function(){
  it('should work this way', function(){
    spyOn( Array.prototype, 'push' ).and.callThrough();
    // expect(1).toBe(1);
  });
});

However, add expect(any expect!) And this will lead to a stack overflow with the following message in the console testem: RangeError: Maximum call stack size exceeded. at http://localhost:7357/testem/jasmine2.js, line 980The html report page fits the specification and then freezes without any actual results.

Ultimately, I would like to do something like this:

describe('Some structure.method', function(){
  it('does not use native push', function(){
    spyOn( Array.prototype, 'push' ).and.callThrough();
    [].push(1); // actually more like `myStructure.myMethod('test')`
    expect( Array.prototype.push ).not.toHaveBeenCalled();
  });
});

Thanks in advance to everyone who can shed light on this oddity. Can I not follow my own prototype methods?

+4
source share
1 answer

-, inorder . , , push push .

[].push(1), , :

   spy = function() {
    callTracker.track({ //<-- Calls tracker to track invocation
      object: this,
      args: Array.prototype.slice.apply(arguments)
    });

, , , .

this.track = function(context) {
  calls.push(context); //Now this again calls the spy
};

, , , push (, , ( Array), push , ): example:

it('does not use native push', function(){
  var arr = [];
  spyOn(arr, 'push' ).and.callThrough();
  arr.push(1);
  expect(arr.push).toHaveBeenCalledWith(1);
});

, ( , ), . , , ( , :)), , , .

+4

All Articles