Sinon: Cannot stub non-existent own property [...]

Created on 4 Mar 2018  路  9Comments  路  Source: sinonjs/sinon

With [email protected].

Consider the following code:

const sinon = require('sinon');

class Foo {
    bar() {
        return 'bar';
    }
}

let foo = sinon.mock(Foo);

sinon.stub(foo, 'bar').returns('foo');

It throws the error Cannot stub non-existent own property bar.

Most helpful comment

Let's finish this issue first, before we tackle the other one.

In the documentation it states:

If you need to stub getters/setters or non-function properties, then you should be using sandbox.stub

This is a limitation in current sinon, that we're working on addressing in sinon@next.

All 9 comments

This is by design. There's plenty discussion of that in the issues, like #1537.

I'll update the documentation to specifically say that it's not possible to stub non-existent properties

@mroderick,

I'll update the documentation to specifically say that it's not possible to stub non-existent properties

But bar exists.

Oh, you've updated the example ... now I see what you mean :)

@mroderick, I'm sorry, yes, I updated it in less than a minute but you're too fast for me! :D

Let's finish this issue first, before we tackle the other one.

In the documentation it states:

If you need to stub getters/setters or non-function properties, then you should be using sandbox.stub

This is a limitation in current sinon, that we're working on addressing in sinon@next.

Thanks a lot for the clarification and pointing to the doc. I missed that part.

@mroderick

The documentation for stub.callsFake() still implies that you can stub nonexistant functions.

@jdmarshall would you like to contribute an update to the documentation to correct this?

See https://github.com/sinonjs/sinon/blob/master/docs/CONTRIBUTING.md

Was this page helpful?
0 / 5 - 0 ratings