使用摩卡,柴和西农测试助焊剂库 [英] Testing flux stores with mocha, chai and sinon

查看:76
本文介绍了使用摩卡,柴和西农测试助焊剂库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与Mocha,Chai和Sinon一起测试Flux商店.我一直在寻找示例,但找不到足够的信息来使我的测试正常工作.

I'm trying to test Flux stores with Mocha, Chai and Sinon. I've been searching for examples but couldn't find enough information to make my tests work.

我尝试了很多事情,但是我很受阻.这就是我现在所拥有的.

I've tried many things but I'm quite blocked. This is what I have now.

来源

MyStore.js

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

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

Dispatcher.js

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

module.exports = new Dispatcher();

MyStoreTest.js

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();
});

有任何提示或帮助吗?谢谢!

Any hint or help? Thanks!

推荐答案

可能对您有用的小更新:在遇到商店状态问题后,我设法使事情正常进行,这一直是一个麻烦.在每次测试后重设.

Small update that might be useful for you: I've managed to get things working after having troubles with the Store's state, which kept being a pain in the butt to reset after each test.

因此,我当前的设置涉及对Register方法进行存根,并监视Dispatch,以便运行操作以将其存储在商店中.

So my current setup involves stubbing the Register method, and spying on the Dispatch in order to run the actions to get them on the store.

我正在使用rewire在每次迭代中获取Store的新副本.所以在beforeEach:

I'm using rewire to grab a fresh copy of the Store on each iteration. So on the 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来将数据存储到存储中,如下所示:

So now on your tests you can get data into the stores by calling the Dispatcher directly, like so:

AppDispatcher.dispatch(actionGetter(data));

到目前为止,这已经证明行之有效!

So far this has proven to work well!

这篇关于使用摩卡,柴和西农测试助焊剂库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆