jeudi 15 septembre 2016

How to stub can call callback using Sinon in a function like this?

Here's my code that I'm trying to mock and stub

function callback(json) {
    // do some object setting to window 
    window.context = json.context;
}
 _.ajax({url: url).done(callback);

It's just a wrapper to ajax call which I'm trying to mock and stub. Now my question is how do I mock .done to call the callback so the test can call callback with the json that I want and I should be able to check if context is set to window object. I'm just using window as an example, I'm not actually setting a global variable.

    mockery.registerAllowable(fourD);
    mockery.registerMock('../helpers', {
      ajax: ajaxStub
    });
    mockery.enable({
      userCleanCache: true,
      warnOnUnregistered: false
    });

    ajaxStub.onCall(0).returns({
      done: () => {
      }
    });

    module = require('../');
  });

But I want the test code to call the real callback but pass the stubbed json.

Aucun commentaire:

Enregistrer un commentaire