简体   繁体   中英

How do I stub/mock a dependency's method copied from “superclass” with sinon

I'm having problems testing a Store in flux, wanting to stub out the dispatcher. I've seen how it's done with jest , but I Want to achieve the same with sinon.

Here's what I have

Source

lib/Dispatcher . Full code here

var invariant = require('./invariant');

//...

function Dispatcher() {
  this.$Dispatcher_callbacks = {};
}

Dispatcher.prototype.register=function(callback) {
  var id = _prefix + _lastID++;
  this.$Dispatcher_callbacks[id] = callback;
  return id;
};


// ...

module.exports = Dispatcher;

Dispatcher :

module.exports.Dispatcher = require('./lib/Dispatcher')

AppDispatcher :

var Dispatcher = require('flux').Dispatcher;
var copyProperties = require('react/lib/copyProperties');
var AppDispatcher = copyProperties(new Dispatcher(), {

  handleViewAction: function(action) {
    this.dispatch({
      source: 'VIEW_ACTION',
      action: action
    });
  }

});

module.exports = AppDispatcher;

ItemStore , the one I want to test

var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var ItemConstants = require('../constants/ItemConstants');
var merge = require('react/lib/merge');

var CHANGE_EVENT = 'change';

var _items = [];

var ItemStore = merge(EventEmitter.prototype, {

  getSoekeresultat: function() {
    return _items;
  },

  emitChange: function() {
    this.emit(CHANGE_EVENT);
  }
});

AppDispatcher.register(function(payload) {
  var action = payload.action;

  switch(action.actionType) {
    case ItemConstants.ITEMS_FETCHED:
      _items = action.payload;
      ItemStore.emitChange();
      break;
    default:
  }

  return true; // No errors.  Needed by promise in Dispatcher.
});

module.exports = ItemStore;

So what I want to stub is AppDispatcher.register. How do I do that?

I've tried doing it several ways, the last I tried was this:

var sinon = require("sinon");
require("jasmine-sinon");

describe("ItemStore", function(){
  var AppDispatcher = require('js/dispatcher/AppDispatcher.js');
  sinon.stub(AppDispatcher, 'register', function () {
    console.log("stub");
  });
  it("should use stub when called directly", function(){
    AppDispatcher.register(function (payload) {
    });
    //outputs "stub"
  });
  it("should use stub when requiring itemstore", function(){
    var ItemStore = require('js/stores/ItemStore.js');
  // doesn't output "stub"
  });

});

Can someone tell me how to stub it?

Although it's not strictly answering your question, you can achieve what you're trying to using proxyquire . It'll allow you to stub the AppDispatcher that is require d inside ItemStore . eg

var proxyquire = require('proxyquire');
var AppDispatcher = require('js/dispatcher/AppDispatcher.js');
var StubbedAppDispatcher = sinon.stub(AppDispatcher)

/*
 * This will load the `ItemStore` module, satisfying it's `AppDispatcher`
 * dependency (the `require('../dispatcher/AppDispatcher')` statement)
 * with our stubbed version.
 */
var ItemStore = proxyquire('js/stores/ItemStore.js', {
  '../dispatcher/AppDispatcher': StubbedAppDispatcher
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM