use*_*827 5 tdd jasmine reactjs jestjs enzyme
我的下面的测试有效,
let output = mount(story);
expect(output.text()).toContain('Superman');
Run Code Online (Sandbox Code Playgroud)
但我什至需要允许蝙蝠侠和蜘蛛侠通过:我需要检查 output.text() 是否有 -> ['Superman','Batman','Spiderman']
不能用
expect(output.text()).toContain(['Superman','Batman','Spiderman']);
Run Code Online (Sandbox Code Playgroud)
output.text() 将包含“超人是最好的”或“蜘蛛侠是最好的”
您可以添加自己的匹配器而不是toContain.
expect.extend({
toContainHero(text) {
let pass = false;
const heroes = ['Superman','Batman','Spiderman'];
heroes.forEach((hero) => {
pass = text.indexOf(hero) !== -1;
})
if (pass) {
return {
message: () =>
`expected hero to be found`,
pass: true,
};
} else {
return {
message: () => `expected hero to be not found`,
pass: false,
};
}
},
});
Run Code Online (Sandbox Code Playgroud)
然后执行以下操作:
expect(output.text()).toContainHero();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13992 次 |
| 最近记录: |