Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    James Gardner
    @james-gardner
    I'd love to be able to use something like:
        const phase = sinon.stub().callArg(1);
            phase.onCall(0) ....
    But how can I modify context from the stub ?
    James Gardner
    @james-gardner
    The answer is not to use spies or mocks.
    Stefan Schneider
    @hinogi
    good evening
    Brandon Istenes
    @brandones

    Hey folks, I'm mocking out a whole object (against the advice of the docs) like

    myStub = sinon.stub(myObj)

    However, that doesn't seem to define a restore method on myStub. How do I restore the original object?

    Brandon Istenes
    @brandones
    Oh wow, I just found out that where the documentation says, e.g. spy.args, it actually means call.args, where call is the sort of object returned by spy.getCall(0).
    Even if for some strange reason args is in fact defined on spies themselves (which it seems to be), the documentation seems misleading with respect to actual usage.
    Adrien Horgnies
    @AdrienHorgnies

    @brandones I advice you to always use the sandbox

    const sandbox = sinon.sandbox.create();
    // do things
    sandbox.verifyAndRestore();

    You can check some tests here

    Adrien Horgnies
    @AdrienHorgnies
    @brandones Regarding args, I didn't find the documentation confusing : Array of arguments received, spy.args[0] is an array of arguments received in the first call..
    But as you do seem confused, maybe some code can help you :
    > spy.thirdCall.args === spy.args[2]
    true
    > spy.getCall(20).args[3] === spy.args[20][3]
    true
    Brandon Istenes
    @brandones
    Yeah, it turned out that the function I was expecting to be called was being called in a promise, and I wasn't waiting for the promise before running my assertions.
    Sandbox seems like just the thing. Perhaps it deserves a more prominent note in the documentation (e.g. in connection with stubbing out whole objects). Thank you for your help!
    Adrien Horgnies
    @AdrienHorgnies
    @brandones I have tons of tests with promises if you want, you can check some if you want.
    Adrien Horgnies
    @AdrienHorgnies
    And yes, I agree with you, sandbox should get more light. I had read the whole documentation plus a tutorial or two about sinon when sinon.sandbox didn't exist. When I discovered it existed by mistake, I thought that it would save me time, make code more readable and more importantly, that would save collaborators from forgeting to verify or restore spies & co.
    Brandon Istenes
    @brandones
    @AdrienHorgnies Nice, thanks!
    Brandon Istenes
    @brandones

    Okay, new problem.

    foo.js

    const doFoo = () => { console.log('bad stuff!') }
    const doBar = () => { doFoo() }
    module.exports = { doFoo, doBar }

    foo.test.js

    const sandbox = require('sinon').sandbox.create()
    const foo = require('./foo')
    
    describe('doBar', () => {
      before(() => {
        sandbox.stub(foo, 'doFoo')
      })
      it('should call doFoo', () => {
        foo.doBar()  // prints "doFoo" to console
      })
    })

    What gives? Why isn't doFoo mocked when doBar calls it?

    Adrien Horgnies
    @AdrienHorgnies
    @brandones If I remember well, you can only wrap functions external to the code you test.
    In your test suit, you're calling a function and mocking another from the same module. That won't work; my guess is that it is because both functions are defined at the same time, giving no opportunity to sinon to wrap doFoo before doBar is defined.
    Anyway, if you must mock functions from the unit you're testing, it smells bad. Either you should move doFoo to another module or you should target more precisely the dangerous section : sandbox.stub(console, 'log');.
    Dimitris Karagiannis
    @mkarajohn
    Hello! I'd like to ask, is it possible to execute a callback once a spy has been called ?
    for example
    const aSpy = sinon.spy(object, 'method');
    aSpy.wasCalled(function() {
       //....do some stuff after the spied method was called
    })
    I have thought of using a stub, saving the original method that is going to get stubbed and then calling the original method along with other code inside a .callsFake()
    Dimitris Karagiannis
    @mkarajohn
    but then I don't know how to get the proper this inside the callsFake in order to apply it to the original method
    Dimitris Karagiannis
    @mkarajohn
    ...ok, it seems like the correct this is passed to the callsFake context. neat
    Adrien Horgnies
    @AdrienHorgnies
    @mkarajohn Look for the onCall method.
    Gintautas Miselis
    @Naktibalda

    I have a problem with undefined methods on mock
    My test code looks like this:

    const mock = sinon.mock(new Xxx(configuration));
    mock.expects('methodA').once().withArgs(sessionId, '', '', '', null).resolves(null);
    new ClassUnderTest(mock, 100);

    ClassUnderTest calls arg1.methodA(); and fails with TypeError: arg1.methodA is not a function

    in debugger I see that arg1 has methodA in expectations and proxies and I see the original object in object property
    Gintautas Miselis
    @Naktibalda
    thanks, I figured it out :) I had to pass the original instance of the class, not the mock
    jeffhall-wk
    @jeffhall-wk
    Hello all! I installed Sinon globally and my node script can't find it. I just receive a 'module not found' error. I see that it is advertised that Sinon doesn't pollute the global namespace, and I'm kind of a newb here. So, is my global installation not being found expected?
    jeffhall-wk
    @jeffhall-wk
    Nevermind! It looks like I shouldn't install it globally. I was trying to mirror another system I found, but they home-roll some other configurations. So I'm all good now :D. Thanks, all!
    Adrien Horgnies
    @AdrienHorgnies
    @jeffhall-wk Indeed, I was wondering why you would install it globally.
    jeffhall-wk
    @jeffhall-wk
    Yep, @AdrienHorgnies I was just being a newb :D
    Alexander Escamilla
    @alexesca

    How can I mock this 'getQbo' function ?

    Have this library

    // quickbooks module.ts
    var QuickBooks = require('node-quickbooks');
    
    export function getQbo (token, companyId, refresh) {
            return new QuickBooks(process.env.QB_CLIENT_ID,
                process.env.QB_CLIENT_SECRET,
                token, /* oAuth access token */
                false, /* no token secret for oAuth 2.0 */
                companyId,
                true, /* use a sandbox account */
                true, /* turn debugging on */
                4, /* minor version */
                '2.0', /* oauth version */
                refresh /* refresh token */);
        }

    Imported like this

    // paymetn.module.ts
    import {getQbo} from "./../../../utils/quickBooks/qbo.module";
    Aaron C
    @aarobc
    Is there documentation somewhere on how to mock or stub out a method that the library we're testing is importing?
    Aaron C
    @aarobc

    Interesting thing I found. sinon works when the library you're mocking exports the mocked property like export const foo = () => {...} but doesn't work if you are exporting like

    module.exports = {
       foo: foo
    }

    any idea what's going on here?

    Aaron C
    @aarobc
    I've just accepted that, but now I'm noticing useFakeTimers()doesn't seem to work with setInterval()
    Aaron C
    @aarobc
    problem was I needed to set it up before setInterval was called
    Is there a way to set up the xhr expected arguements with the xhr server? say I don't have a callback coming back from the method
    jeffhall-wk
    @jeffhall-wk
    @alexesca You should be able to mock the whole QuickBooks object like this
    var mock = sinon.mock(QuickBooks);
    Or you can stub just that one method like this:
    var stub = sinon.stub(QuickBooks, “getQbo”);
    let me know if that doesn’t work
    Actually, you import the object as “getQbo”. My bad, try these:
    var mock = sinon.mock(getQbo); var stub = sinon.stub(getQbo, “getQbo”);
    David Schwartz
    @dbschwartz
    Hey everyone. Just curious if anyone had tips for spying on objects. I know how to spy on functions, but if you want to spy on say the request object in an Express middleware it doesn't seem as intuitive. I've tried spying on the middleware function, but when I do spy.thisValues I receive undefined
    David Schwartz
    @dbschwartz
    Take that back, my actual problem is I need to assert on the object after the test has finished
    David Schwartz
    @dbschwartz
    N/m the first problem still applies. Trying to spy on the object
    jeffhall-wk
    @jeffhall-wk
    Is there a quick way to restore all things that are stubbed/mocked?
    jeffhall-wk
    @jeffhall-wk
    I
    I’m not seeing anything about that here http://sinonjs.org/releases/v4.1.3/spies/
    I think you might have to create multiple spies for each function on the object.
    Alexander Escamilla
    @alexesca
    Thank you. @jeffhall-wk I did not see your post until now. What I did was to inject QuickBooks, so i could mock it easily. Like Angular DI
    jeffhall-wk
    @jeffhall-wk
    Neat! Can you post an example? Would love to see how that worked out.
    @alexesca ^