Unit testing 如何测试存储/操作?

Unit testing 如何测试存储/操作?,unit-testing,reactjs,reactjs-flux,jestjs,Unit Testing,Reactjs,Reactjs Flux,Jestjs,我需要测试alt存储;为此,我使用alt dispatcher引发了一个操作,并尝试在操作侦听器中侦听它。但是我的alt对象没有定义 行动类: var alt = require('../alt'); var LocationSource = require('../sources/LocationSource'); class LocationActions { updateLocations(locations) { this.dispatch(locations)

我需要测试alt存储;为此,我使用alt dispatcher引发了一个操作,并尝试在操作侦听器中侦听它。但是我的alt对象没有定义

行动类:

var alt = require('../alt');
var LocationSource = require('../sources/LocationSource');

class LocationActions {
    updateLocations(locations) {
        this.dispatch(locations);
    }

    fetchLocations() {
        this.dispatch();
        var actionDispatcher = this;

        LocationSource.fetchLocations().then(function(items) {

            actionDispatcher.actions.updateLocations(items);

        });
    }

    locationsFailed(errorMessage) {
        this.dispatch(errorMessage);
    }

    favoriteLocation(location) {
        this.dispatch(location);
    }

    fetchedLocation(location) {
        this.dispatch(location);
    }

    fetchLocation(locationId) {

        var actionDispatcher = this;

        actionDispatcher.dispatch();

        LocationSource.fetchLocation(locationId).then(function(item) {

            actionDispatcher.actions.fetchedLocation(item);

        });
    }
}

module.exports = alt.createActions(LocationActions);
我的商店:

var alt = require('../alt');
var LocationActions = require('../actions/LocationActions');

class FavoritesStore {
    constructor() {
        this.locations = [];

        this.bindListeners({
            addFavoriteLocation: LocationActions.FAVORITE_LOCATION
        });
    }

    addFavoriteLocation(location) {
        this.locations.push(location); 
    }

    onRemoveFavoriteLocation(){

    }

}

module.exports = alt.createStore(FavoritesStore, 'FavoritesStore');
我的Alt js:

var Alt = require('alt');
var alt = new Alt();
var chromeDebug = require('alt/utils/chromeDebug')

chromeDebug(alt);

module.exports = alt;
以下是测试文件:

"use strict";
jest.dontMock('../src/js/stores/FavoritesStore');
jest.dontMock('../src/js/actions/LocationActions');
jest.dontMock('../src/js/alt');
import alt from '../src/js/alt';
var fav =require('../src/js/stores/FavoritesStore');

import LocationActions from '../src/js/actions/LocationActions';

describe('fav', function() {

    beforeEach(function() {
        sinon.spy(alt.dispatcher, 'dispatch');

        alt.dispatch(LocationActions.FAVORITE_LOCATION, {id:15, name:'khi'});
    });

    afterEach(function() {
        // clean up our sinon spy so we do not affect other tests
        alt.dispatch.restore();

    });
    it('add a location to favorites list ', function() {

        var id: 15, name: 'San Francisco' ,
        action = LocationActions.FAVORITE_LOCATION;
        console.log({id:15, name:'khi'});
        // fire the action
        LocationActions.favoriteLocation({id:15, name:'khi'});

    });
});

不确定这是否已经解决,但ES6
import
s和jest mocking指令存在问题。Babel/Babel jest(我猜您正在使用的工具)将在编译期间提升(拉到最顶端)和
import
-语句。这意味着
从“../src/js/alt”导入altjest.dontMock('../src/js/alt')之前运行code>。这可能导致
alt
被模拟。