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

How to return fake data from spy.on(obj, 'funcName') when called ? #48

Closed
narainsagar opened this issue Mar 4, 2016 · 3 comments
Closed

Comments

@narainsagar
Copy link

I don't know is there any function or not!
Please let me know is it possible?

Something like that:
spy(obj, 'funcName').and.returnValue(5); // spy will return a fake data when called`

@scaljeri
Copy link

+1 I would really like to know this too!

@lucasfcosta
Copy link
Member

Hello guys, thanks for the issue!
For what I can see chai-spies is not able change the return value of a function. Chai's focus is actually doing assertions, so you can only run things related to assertions on spies.
To create stubs for your functions you can use SinonJS.


EDIT: I'm not sure if I totally understand your doubt, but I was reading this again and actually we have this:

    /**
     * # chai.spy.returns (function)
     *
     * Creates a spy which returns static value.
     *
     *      var method = chai.spy.returns(true);
     *
     * @param {*} value static value which is returned by spy
     * @returns new spy function which returns static value
     * @api public
     */

I hope it is what you guys were looking for.

@keithamus
Copy link
Member

Hey @narainsagar @scaljeri and @lucasfcosta 😄

Right now the spy.on API doesn't allow you to override the functionality. Some discussion has gone on about this in #38 (see #38 (comment)). The alternatives so far are:

Array.prototype.push = chai.spy(function () { return undefined });
Array.prototype.push = chai.spy.returns(undefined);
Array.prototype.push = function () { return undefined; }
chai.spy.on(Array.prototype, 'push');

We've discussed before about having extra methods on the spy interface, but we'd like to move away from that (#38 has some on this, as does this comment in #35: #35 (comment)).

I'll close this, because it's on the radar and will likely come as a result of #38. If you want to help out, continuing the discussion there would be awesome!

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

4 participants