【问题标题】:Testing flux stores with mocha, chai and sinon使用 mocha、chai 和 sinon 测试助焊剂商店
【发布时间】:2016-01-25 12:00:45
【问题描述】:

我正在尝试使用 Mocha、Chai 和 Sinon 测试 Flux 商店。我一直在寻找示例,但找不到足够的信息来进行测试。

我尝试了很多东西,但我很受阻。这就是我现在所拥有的。

来源

MyStore.js

var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var MyStoreConstants = require('../constants/MyConstants');
var assign = require('object-assign');

var CHANGE_EVENT = 'change';

var _results = [];

function setResults(values) {
    _results = values;
}

var MyStore = assign({}, EventEmitter.prototype, {
    getResults: function () {
        return _results;
    },
    emitChange: function () {
        this.emit(CHANGE_EVENT);
    },
    addChangeListener: function (callback) {
        this.on(CHANGE_EVENT, callback);
    },
    removeChangeListener: function (callback) {
        this.removeListener(CHANGE_EVENT, callback);
    }
});

AppDispatcher.register(function (action) {
    switch (action.actionType) {
        case MyConstants.SET_RESULTS:
            setResults(action.values);
            MyStore.emitChange();
            break;
        default:
            console.log('unknown action');
     }
});

module.exports = MyStore;

MyActions.js

var AppDispatcher = require('../dispatcher/AppDispatcher');
var MyConstants = require('../constants/MyConstants');

var MyActions = {
    setResults: function (values) {
        AppDispatcher.dispatch({
            actionType: MyConstants.SET_RESULTS,
            values:values
        })
    }
};

module.exports = MyActions;

Dispatcher.js

var Dispatcher = require('flux').Dispatcher;

module.exports = new Dispatcher();

MyStoreTest.js

import {
       sinon,
       assert,
       expect
} from '../test_helper';

describe('MyStore', function() {

    var MyStore, AppDispatcher, registerSpy, MyConstants, callback;

beforeEach(() => {
    MyConstants = require('../../../src/js/constants/MyConstants.js');
    MyStore = require("../../../src/js/stores/MyStore.js");
    AppDispatcher = require("../../../src/js/dispatcher/AppDispatcher.js");
    registerSpy = sinon.spy(AppDispatcher, "register");
    AppDispatcher.register(MyStore);
    callback = registerSpy.lastCall.args[0];
});

afterEach(function () {
    registerSpy.restore();
});

it('registers a callback with the dispatcher', function() {
    expect(registerSpy.callCount).to.equal(1);
});

it('initialize with empty results', function() {
   expect(MyStore.getResults().length).to.equal(0);
});

it('fill with some results after action called', function() {
    var someResults = [3, 5, 6, 4];

    var actionSetResults = {
        actionType: MyConstants.SET_RESULTS,
        values: someResults
    };

/* I would like to fire the action and check that after the action 
is called, the value in the store changes. 
I've tried different things but none of them are working. 
probably  something about requiring something or mocking/ not mocking
something important */

   ---> send the action actionSetResults to the store

    var results = MyStore.getResults();
});

任何提示或帮助?谢谢!

【问题讨论】:

  • 请不要在问题正文中编辑解决方案。如果您想发布解决方案,请将其发布为答案。确保你解释了你的解决方案,并强调它说明了什么超出了你已经接受的答案。

标签: testing reactjs mocha.js store flux


【解决方案1】:

可能对您有用的小更新:在 Store 的状态出现问题后,我设法让事情正常运行,每次测试后重置它总是让人头疼。

因此,我当前的设置涉及对 Register 方法进行存根,并监视 Dispatch 以运行操作以将它们放到商店中。

我使用rewire 在每次迭代中获取 Store 的新副本。于是上beforeEach

TemplateStore = rewire('../../src/js/stores/TemplateStore');
registerSpy = sinon.stub(AppDispatcher, 'register');
dispatchSpy = sinon.spy(AppDispatcher, 'dispatch');
AppDispatcher.register(TemplateStore);
callback = registerSpy.lastCall.args[0];

然后在afterEach

registerSpy.restore();
dispatchSpy.restore();

所以现在在您的测试中,您可以通过直接调用 Dispatcher 将数据放入存储中,如下所示:

AppDispatcher.dispatch(actionGetter(data));

到目前为止,这已被证明效果很好!

【讨论】:

  • 显然我的问题是我在监视寄存器而不是存根。现在工作正常。谢谢!
猜你喜欢
  • 2015-02-06
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 2020-10-11
相关资源
最近更新 更多