所以我正在测试一个组件,我需要它与我的商店一起行动以获得正确的可测试行为。我已经读过,因为“视图”需要商店才能运行,所以我不需要模拟商店,也不需要模拟“对象分配”的东西。
测试代码摘录:
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
}
);
以及视图中的一些代码:
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
......在我的改变听众线上,尽管我(认为我)在“不嘲笑”事情时正在做正确的事情。
任何人都知道为什么我会得到这个,仍然吗?