Unit testing 用Jest测试React存储

Unit testing 用Jest测试React存储,unit-testing,reactjs,reactjs-flux,jestjs,Unit Testing,Reactjs,Reactjs Flux,Jestjs,我正在尝试使用Jest测试我的React存储,我遵循Flux架构,但不确定何时需要模拟存储函数。为了说明我的观点,这里是我的商店代码的一部分: export default class AdminStore extends EventEmitter { constructor() { super(); this.users = []; this.userTotal = 0; this.profile = {}; Adm

我正在尝试使用Jest测试我的React存储,我遵循Flux架构,但不确定何时需要模拟存储函数。为了说明我的观点,这里是我的商店代码的一部分:

export default class AdminStore extends EventEmitter {
   constructor() {
       super();
       this.users = [];
       this.userTotal = 0;
       this.profile = {};

       AdminDispatcher.register((payload) => {
           switch (payload.eventName) {
               case AdminConstants.USER_TOTAL:
                   this.userTotal = payload.total;
                   this.emit('change');
               break;

               case AdminConstants.ALL_USERS:
                   this.users = payload.users;
                   this.emit('change');
               break;

               case AdminConstants.FETCH_USER:
                   this.profile = payload.profile;
                   this.emit('change');
               break;

               default:

               break;
           }
           return true;
       });
   }

   syncUsers() {
       return this.users;
   }

   syncUserTotal() {
       return this.userTotal;
   }

   syncProfile() {
       return this.profile;
   }
}
下面是我的Jest测试代码片段:

jest.dontMock('../AdminStore.js');
jest.dontMock('../../constants/AdminConstants.js');

describe('AdminStore', () => {
    let callback;
    let AdminStore;
    let AdminDispatcher;
    let AdminConstants = require('../../constants/AdminConstants.js');

    beforeEach(() => {
        AdminStore = require('../AdminStore.js');
        AdminDispatcher = require('../../dispatcher/AdminDispatcher.js');
        callback = AdminDispatcher.register.mock.calls[0];
    });
    it ('should initialize with a user total of 0', () => {
        let total = AdminStore.syncUserTotal();
        expect(total).toEqual(0);
    });
});
此测试的Jest输出如下所示:

● AdminStore › it should initialize with a user total of 0
  - TypeError: undefined is not a function
    at Spec.<anonymous> (/Users/seanchen/karma/admin-new/src/js/stores/__tests__/AdminStore-test.js:34:32)
    at jasmine.Block.execute (/Users/seanchen/karma/admin-new/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
    at jasmine.Queue.next_ (/Users/seanchen/karma/admin-new/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
    at null._onTimeout (/Users/seanchen/karma/admin-new/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
1 test failed, 1 test passed (2 total)
Run time: 3.493s

是否需要模拟AdminStore.syncUserTotal?我真的不知道为什么它是未定义的。

您没有AdminStore的实例

AdminStore正在导出类本身,而不是该类的实例

试试像这样的东西

it ('should initialize with a user total of 0', () => {
    let adminStore = new AdminStore();
    let total = adminStore.syncUserTotal();
    expect(total).toEqual(0);
});

你能把代码贴在第34行吗。我们看不到行号第34行是let total=AdminStore.syncUserTotal;在笑话代码中。