Javascript TypeError:无法读取属性';呼叫';未定义的
我正在尝试在react应用程序中为我的存储编写单元测试。Javascript TypeError:无法读取属性';呼叫';未定义的,javascript,reactjs,Javascript,Reactjs,我正在尝试在react应用程序中为我的存储编写单元测试。 单元测试看起来像: import FRIENDS from '../../constants/friends'; import FriendsStore from '../friends_store'; jest.dontMock('../../constants/friends'); jest.dontMock('../friends_store'); describe('FriendsStore', () => {
单元测试看起来像:
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 CLI v0.4.0
脚本/存储/测试失败\uuuuuu\friends\u store-test.js(0.811s)
● FriendsStore›它应该初始化为没有好友项目
-TypeError:无法读取未定义的属性“calls”
在规范中(/Volumes/Developer/reactjs/app5/scripts/stores/__-tests\u_/friends\u-store-test.js:33:41)
在jasmine.Block.execute(/Volumes/Developer/reactjs/app5/node_modules/jest cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
在jasmine.Queue.next(/Volumes/Developer/reactjs/app5/node_modules/jest cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
空时(/Volumes/Developer/reactjs/app5/node\u modules/jest cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
at Timer.listOnTimeout[as onttimeout](timers.js:112:15)
1个测试失败,0个测试通过(共1个)
运行时间:1.028s
似乎jest找不到调用
属性。我做错了什么?我也有类似的问题
进行以下更改可以解决此问题:
beforeEach(function () {
AppDispatcher = require('../../dispatcher/app_dispatcher');
FriendsStore = require('/path/to/store'); //**load in store**
callback = AppDispatcher.register.mock.calls[0][0];
});
使用ReactJSV15.2.1和Jest v14.0.0
这也适用于ReactJS v14。实际上,这意味着mock未定义,未定义没有您在beforeach中期望的calls属性。。。那么,当代码到达这个语句时,您可能还没有定义一个mock?但是我不需要定义mock,jest会自动为我这样做吗?我看一下上面的例子,它也没有定义mock。这是使用Jest的主要困惑之一。它不是自动模拟你需要的所有东西,而是模拟你需要的东西的依赖关系。当我下载todo mvc并使用jest进行测试时,它的工作非常有魅力。但是当我试图自己构建测试时,它总是告诉我属性调用不存在,这太奇怪了。我正在使用babel将es6代码转换为es5代码,也许这就是问题所在?您找到解决方案了吗?这使我无法读取未定义的属性“mock”。这部作品是在什么版本的《通量与玩笑》上创作的?