【发布时间】:2017-12-29 07:57:14
【问题描述】:
我正在进行快照测试,但由于随机生成的不同时间和 id 而导致错误失败。 那么,我如何忽略测试中的那些部分或模拟它们 以下是测试给出的一些错误:
<span>
- Today at 10:21 AM
+ Today at 12:34 PM
</span>
和
<input
disabled={false}
- id="undefined-Search-undefined-24783"
+ id="undefined-Search-undefined-63537"
onBlur={[Function]}
onChange={[Function]}
onFocus={[Function]}
这是代码中输出时间的部分
<p style={profileStyle}>
<span>
{moment().
subtract(20, 'm').
calendar()}
</span>
和
<span>{moment('1/12/14').calendar()}</span>
这个输出随机生成的id
<TextField
hintText="Search"
fullWidth
/>
这是我的测试代码
test('renders correctly', () => {
const component = renderer.create(<MuiThemeProvider muiTheme={getMuiTheme()}>
<CheckinHistory/></MuiThemeProvider>);
const tree = component.toJSON();
console.log('tree is : ', tree);
expect(tree).toMatchSnapshot();
});
我尝试使用这个来模拟
moment().subtract(20, 'm').calendar() = jest.fn()
但是 linter 会出错,而且看起来也不正确。 那么,我哪里错了?
【问题讨论】:
标签: reactjs unit-testing enzyme