Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Joakim Bajoul Kakaei
    @Todai88
    Is this community still active?
    Lars Gyrup Brink Nielsen
    @LayZeeDK
    Not very much.
    Sasha Boginsky
    @sashadev-sky
    hey! Does anyone here have experience writing tests in sinon with Ruby on Rails and could help me with some questions I have
    Richard Annand
    @rj175
    Not sure if this channel is still active but I am looking for some help with the sinon.stub method
    Derek W. S. Gray
    @derekwsgray
    @rj175 what's the question?
    Brad Compton (he/him)
    @Bradcomp
    I lurk still too
    Arthur Neuman
    @mets11rap
    test('loopFunction', async (t) => {
      function loopFunction () {
        return true
      }
    
      const spy = sinon.spy(loopFunction)
    
      const agent = new Agent({
        Eris: PDiscord,
        agentOptions: {
          loopFunction,
          loopInterval: 100
        }
      })
    
      await delay(200)
    
      loopFunction() // debug
      console.log(spy.callCount)
      t.true(spy.calledTwice)
    
      return agent
    })
    console.log(spy.callCount) returns 0. Why?
    Derek W. S. Gray
    @derekwsgray
    @mets11rap You didn't call the spy
    The spy is a new function
    You should be passing spy in your agentOptions, not loopFunction
    Babita-Sandooja
    @Babita-Sandooja

    Hi
    Can someone please help me with below prob:
    I need to stub xyz function
    here is my source file:

    module.exports = (() => { 'use strict'; return ((browser) => { const abc = (duration = constants.SHORT_WAIT) => { console.log('in source : ', xyz()); if (xyz()) { browser.pause(duration); } }; return { abc, xyz }; }); })();

    I stubbed it in test file and showing correct value.
    But in source file, it's not giving stubbed value

    Derek W. S. Gray
    @derekwsgray
    @Babita-Sandooja I don't understand the question.
    Babita-Sandooja
    @Babita-Sandooja

    I think this stack overflow post explains it: https://stackoverflow.com/questions/35162659/sinon-function-stubbing-how-to-call-own-function-inside-module

    solution they suggested is to refactor the module itself..
    is there some other way as well?

    I think rewire but haven't used it before..
    Brad Compton (he/him)
    @Bradcomp
    Rewire probably won't work because you aren't stubbing the top level function. Is it possible to stub out the whole module, returning the stubbed methods?
    Babita-Sandooja
    @Babita-Sandooja
    I need to test 1 method in that module.. so stubbing whole module :thought_balloon:
    I don't think that's an option?
    Derek W. S. Gray
    @derekwsgray
    sinon.replace ?
    Babita-Sandooja
    @Babita-Sandooja
    oh.. so it would replace in the module also
    Derek W. S. Gray
    @derekwsgray
    yes
    declare the stub, then use .replace(module, theStub)
    Babita-Sandooja
    @Babita-Sandooja
    cool.. Let me try with this :thumbsup:
    Babita-Sandooja
    @Babita-Sandooja
    No, doesn't seem to work
    var fake = sinon.fake.returns(true); sinon.replace(sourceFile, 'xyz', fake);
    when I do console.log(xyz()), It's saying xyz is not defined
    when I do sourceFile.xyz() It's giving true
    but gives false in source file where it's just xyz()
    Derek W. S. Gray
    @derekwsgray
    can you paste your module in a formatted code block
    Babita-Sandooja
    @Babita-Sandooja
    Just FYI, code refactoring as suggested in stack overflow link worked!
    Derek W. S. Gray
    @derekwsgray
    Oh, I thought that wasn't an option - yes by all means, refactor!
    Babita-Sandooja
    @Babita-Sandooja
    Derek W. S. Gray
    @derekwsgray
    Encouraging refactors is a large part of writing unit tests anyway
    Babita-Sandooja
    @Babita-Sandooja
    @derekwsgray PR is not approved yet though..
    so you think that refactor is better? like how?
    I did it just to make it work.. but not sure how it's better than what I had already!
    Derek W. S. Gray
    @derekwsgray
    In that SO example, the function has already been assigned. It can't be replaced
    Babita-Sandooja
    @Babita-Sandooja
    ya.. same seemed to be the issue with me
    but we have sinon.replace :thought_balloon:
    even that can't override?
    Derek W. S. Gray
    @derekwsgray
    But you aren't replacing anything
    the original function has been assigned.
    you aren't replacing that assignment
    Babita-Sandooja
    @Babita-Sandooja
    hmm.. you mean I am doing it wrong or is it not possible?
    Derek W. S. Gray
    @derekwsgray
    You need the refactor
    Babita-Sandooja
    @Babita-Sandooja
    yeah
    that's the only way to unit test it?
    Derek W. S. Gray
    @derekwsgray
    To "unit test" a unit has to be isolated. Yours isn't.
    Babita-Sandooja
    @Babita-Sandooja
    yup
    Derek W. S. Gray
    @derekwsgray
    the refactor gives you better isolated and testable code. I'd be good to do regardless
    Babita-Sandooja
    @Babita-Sandooja
    yes yes
    Derek W. S. Gray
    @derekwsgray
    easier for future refactoring, dependency-injection etc.
    I use bottle.js for dependency injection.
    Babita-Sandooja
    @Babita-Sandooja
    okay
    I haven't used it ever.. just read about it
    Looks cool
    Thanks for sharing and all your help!