sinonjs/sinon

Spy's thisValue not pointing to instance when spying constructor

Open

#1 683 ouverte le 9 févr. 2018

Voir sur GitHub
 (11 commentaires) (0 réactions) (0 assignés)JavaScript (764 forks)batch import
BugDifficulty: MediumHelp wantedRegressionhacktoberfestpinned

Métriques du dépôt

Stars
 (9 548 stars)
Métriques de merge PR
 (Merge moyen 6j 16h) (15 PRs mergées en 30 j)

Description

This snippet works fine with Sinon 2.0.0 but fails with Sinon 4.2.2:

it('returns correct thisValue', function() {
    window.SomeClass = function() {
        this.counter = 0;
        this.inc = function() {
            this.counter++;
        };
        this.getCounter = function() {
            return this.counter;
        };
    };

    var spy = sinon.spy(window, 'SomeClass');
    var some = new window.SomeClass();
    some.inc();

    expect(spy.thisValues[0].counter).toEqual(1);
    expect(spy.thisValues[0].getCounter()).toEqual(1);
    some.inc();
    expect(spy.thisValues[0].counter).toEqual(2);
    expect(spy.thisValues[0].getCounter()).toEqual(2);
});

It looks like the thisValues[0] object is a proxy and does not refer to the actual instance of some.

This is the error with Sinon 4.2.2:

Expected undefined to equal 1
TypeError: undefined is not a function (evaluating 'spy.thisValues[0].getCounter()')

Guide contributeur