Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sinon@next: sinon.replace only works on own property #1695

Closed
mantoni opened this issue Feb 21, 2018 · 3 comments
Closed

sinon@next: sinon.replace only works on own property #1695

mantoni opened this issue Feb 21, 2018 · 3 comments

Comments

@mantoni
Copy link
Member

mantoni commented Feb 21, 2018

While playing with #1586, I noticed that sinon.replace(obj, 'prop', fake) only works if 'prop' is an own property of obj. I can only make this work with sinon.replace(Object.getPrototypeOf(obj), 'prop', fake).

sinon.replace should allow to replace properties up the prototype chain, like sandbox.stub does.

@mroderick
Copy link
Member

mroderick commented Feb 25, 2018

I tried adding a test to reproduce your scenario

it("should replace property on prototype", function () {
    var Person = function (name) {
        this.name = name;
    };
    var Customer = function (name) {
        Person.call(this, name);
    };

    Customer.prototype = Object.create(Person.prototype);
    Customer.prototype.constructor = Customer;

    var customer = new Customer("Some Name");
    var replacement = "Some Other Name";

    this.sandbox.replace(customer, "name", replacement);

    assert.equals(customer.name, replacement);
});

However, this passes with no changes to sandbox.js.

I don't think I understood your description correctly. Can you suggest an improvement to the test, that would make it clear what replace is currently not capable of?

@mantoni
Copy link
Member Author

mantoni commented Feb 25, 2018

@mroderick The reason your example doesn't fail is because this.name = name creates an own property of the object. If you'd add a method to the Person prototype and try to replace it, that would fail. You don't need the derived Customer.

Anyway, I've created #1704 to illustrate the issue with a failing test.

@mroderick
Copy link
Member

Fixed with #1705

mroderick pushed a commit that referenced this issue Mar 30, 2018
mroderick pushed a commit to mroderick/sinon that referenced this issue Apr 27, 2018
mroderick pushed a commit to mroderick/sinon that referenced this issue Apr 27, 2018
mroderick pushed a commit to mroderick/sinon that referenced this issue Apr 27, 2018
mroderick pushed a commit that referenced this issue Apr 30, 2018
franck-romano pushed a commit to franck-romano/sinon that referenced this issue Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants