Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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 ^
    Andrew Vaughan
    @andrewvaughan
    hey all, I was wondering if someone might be able to help me get past a hurdle I'm trying to tackle with sinon
    I'm trying to build an Alexa skill, I have most of the skill tested with Mocha/Should/Sinon - but they have this really weird architecture for their "Intent" code, which actually is the meat of the skill
    They use "this" inside an object that isn't a class
    I've tried stubbing the response calls, I've tried to call it with .call and .apply to set what this should be, and a number of other things, but this is always {} inside the intent object
    Any idea how I might be able to mock or stub the properties that the object is calling on this inside that object so I can test if they're called?
    For the lazy who don't want to go to the link, here's an example of what I'm talking about
    const handlers = {
        'HelloWorldIntent' : function() {
            //emit response directly
            this.emit(':tell', 'Hello World!');
        }
    };
    Andrew Vaughan
    @andrewvaughan
    Unfortunately, trying to mock emit in this example throws TypeError: Attempted to wrap undefined property emit as function - since they don't build a proper class
    Andrew Vaughan
    @andrewvaughan
    Or spy, rather
    Andrew Vaughan
    @andrewvaughan
    Resolved it myself - kindof - for those interested, I couldn't call apply because I was using arrow functions in my module
    If I revert them to old-school anonymous functions, it overrides this as planned - gotta do some reading as to why the arrows weren't working
    Alexander Escamilla
    @alexesca
    // Import qbs
    import {getQbo} from "./../../utils/quickBooks/qbo.module";
    // My new instance 
    const invoiceModule: InvoiceModule = new InvoiceModule(getQbo);
    // My class 
    export class InvoiceModule {
    
        qbo: any;
        addressModule: AddressModule = new AddressModule();
        customerModule: CustomerModule = new CustomerModule();
        emailModule: EmailModule = new EmailModule();
    
        constructor(getQbo) {
            const token = process.env.QB_APP_TOKEN;
            const companyId = process.env.QB_COMPANY_ID;
            const refresh = process.env.QB_APP_TOKEN_REFRESH;
            this.qbo = getQbo(token, companyId, refresh);
        }
    }
    @jeffhall-wk sorry for the wait. Hope it helps
    jeffhall-wk
    @jeffhall-wk
    @alexesca thanks!
    Alexander Escamilla
    @alexesca
    I use nodeJS, typescript, sinonTest, and rxjs-marble in my project. I am trying to stub my funcitons, but sinon does not mock all of them. How can I use sinon and marbles at the same time?
    before(marbles(function(m) {
            composeInvoiceEmailHtmlMock = sinon.stub(InvoiceModule.prototype,'composeInvoiceEmailHtml')
             .returns('<p>It was mocked</p>');*/
            composeCustomerMock$ = sinon.stub(InvoiceModule.prototype, 'composeCustomerAndCreateCustomerInQBO')
                .returns(m.cold('--a|', {a: customerMock}));
            composeInvoiceMock$ = sinon.stub(InvoiceModule.prototype, 'composeInvoiceAndCreateInvoiceInQBO')
                .returns(m.cold('--a|', {a: invoiceMock}));
            sendEmailMock$ = sinon.stub(InvoiceModule.prototype, 'sendEmail$')
                .returns(m.cold('--a|', {a: 'SENT_EMAIL'}));
        }));
    
        after(function () {
            composeCustomerMock$.restore();
            composeInvoiceMock$.restore();
            sendEmailMock$.restore();
        })
    
    
        it('should create an invoice in QBO', marbles(function(m) {
                const b = m.cold('------b', {b: 'SENT_EMAIL'});
                m.expect(invoiceModule.createCustomerAndInvoiceInQBO$(rawDocument)).toBeObservable(b);
        }));
    I wanna be able to use sinonTest
    it('should do something with stubs', sinon.test(function() {
      var stub = this.stub($, 'post');
    
      doSomething();
    
      sinon.assert.calledOnce(stub);
    });
    Alexander Escamilla
    @alexesca
    If you guys ever need to test observables with marbles and sinon, this is how i did it,
    var sinon = require('sinon');
    let sinonTestFactory = require("sinon-test");
    let sinonTest = sinonTestFactory(sinon);
    
    it('should test the createECHeckCharge function', sinonTest(function() {
            const that = this;
            marbles(m => {
                that.stub(request, 'post').yields(null, {OK: true}, {status: 'CAPTURED'});
                m.expect(paymentModule.createCharge({})).toBeObservable(m.cold('a', {a: {status: 'CAPTURED'}}))
            })()
        }));
    Remco Gubbels
    @Bubbit
    I'm currently trying to rewrite a piece of codebase from callbacks to promises, but sadly the tests are now failing due to the callFake method isn't being picked up, anyone experienced something like that before?
    Ifemide
    @bwoodlt
    Guys