我正在尝试在React应用程序中为我的商店编写单元测试。
单元测试如下:
import FRIENDS from '../../constants/friends';
import FriendsStore from '../friends_store';
jest.dontMock('../../constants/friends');
jest.dontMock('../friends_store');
describe('FriendsStore', () => {
let AppDispatcher;
let callback;
let addFriends = {
actionType: FRIENDS.ADD_FRIENDS,
name: 'Many'
};
let removeFriend = {
actionType: FRIENDS.REMOVE_FRIENDS,
id: '3'
};
beforeEach(function () {
AppDispatcher = require('../../dispatcher/app_dispatcher');
callback = AppDispatcher.register.mock.calls[0][0];
});
it('Should initialize with no friends items', function () {
var all = FriendsStore.getAll();
expect(all).toEqual([]);
});
});
当我使用
jest
语句执行时,我得到了错误消息:Using Jest CLI v0.4.0
FAIL scripts/stores/__tests__/friends_store-test.js (0.811s)
● FriendsStore › it Should initialize with no friends items
- TypeError: Cannot read property 'calls' of undefined
at Spec.<anonymous> (/Volumes/Developer/reactjs/app5/scripts/stores/__tests__/friends_store-test.js:33:41)
at jasmine.Block.execute (/Volumes/Developer/reactjs/app5/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
at jasmine.Queue.next_ (/Volumes/Developer/reactjs/app5/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
at null._onTimeout (/Volumes/Developer/reactjs/app5/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test failed, 0 tests passed (1 total)
Run time: 1.028s
似乎是,开 Jest 找不到
calls
属性。我究竟做错了什么? 最佳答案
我有一个类似的问题。
进行以下更改应该可以解决此问题:
beforeEach(function () {
AppDispatcher = require('../../dispatcher/app_dispatcher');
FriendsStore = require('/path/to/store'); //**load in store**
callback = AppDispatcher.register.mock.calls[0][0];
});
使用ReactJS v15.2.1和Jest v14.0.0
这也适用于ReactJS v14。
关于javascript - TypeError:无法读取未定义的属性 'calls',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30060112/