所以我正在测试一个组件,我需要它与我的商店一起行动,以获得正确的可测试行为。我已经读到,因为"视图"需要商店的功能,我需要不模拟商店,也要不模拟"对象分配"的东西。
测试代码摘录:
jest.dontMock('object-assign');
jest.dontMock('../StationSearch.jsx');
jest.dontMock('../../stores/StationStore.js');
describe('StationSearch', function() {
var StationSearch;
var stationSearch;
var React;
var TestUtils;
beforeEach(function() {
React = require('react/addons');
TestUtils = React.addons.TestUtils;
StationSearch = require('../StationSearch.jsx');
stationSearch = TestUtils.renderIntoDocument(<StationSearch />);
});
it('is rendered without value by default', function() {
// code here
}
);
和View中的一些代码:
var React = require('react');
var StationStore = require('../stores/StationStore');
var StationSearch = React.createClass({
componentDidMount: function() {
StationStore.addChangeListener(this._onChange);
},
});
现在,当运行测试时,我得到
TypeError: Cannot call method 'addChangeListener' of undefined
…在我的改变听众线,尽管我(认为我)做正确的事情,当它涉及到"不嘲笑"的事情。
还有人知道为什么我能得到这个吗?
可能迟到了,但是…
store未定义,因为object-assign正在被模拟。使用以下方法之一取消模拟
1)将这行添加到您的jest测试文件的顶部
jest.dontMock('object-assign');
2)将此添加到您的包中。json文件
"jest": {
"unmockedModulePathPatterns": [
"object-assign"
]
},