Sinon.JS – How can I get arguments from a stub?

Thanks to Matt Mokary (if you want to submit an answer I’ll give you the credit for this one)

I edited my test as follows…

it("Can show a fail message", Sinon.test(function() {        
    $("body").append(`<a class="js-delete-button" data-id="123" data-delete-uri="/delete/123">Delete</a>`);
    let objUt = new DeleteButton($(".js-delete-button").get()[0]);
    let bootboxAlertStub = this.stub(Bootbox, 'alert');
    objUt.showFailed();
    Sinon.assert.called(bootboxAlertStub);
    let options = bootboxAlertStub.getCall(0).args[0];
    expect(options.message).to.equal(objUt.errorMessage);
}));

The thing that fixed it was, as Matt suggested, using…

bootboxAlertStub.getCall(0);

with the slight adjustment of

bootboxAlertStub.getCall(0).args[0];

to get the first argument (which is the options object).

As an aside, discovering that I can use console.log to see what’s going on when I’m running a test through Phantom and Karma was both a revelation and a headslapping moment.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)