检查字符串是否包含列表中的子字符串

问题描述 投票:0回答:2

我正在使用 Chai.js 编写一些自动化测试。我有一根绳子:

url(http://somewhere.com/images/myimage.png)

我想做这样的事情:

expect(thatSelectedItem).contains.any('jpg', 'png', 'gif')

但是在 chai.js 中似乎找不到任何东西

任何有任何建议 - 请阅读页面http://chaijs.com/api/bdd/但是没有运气。

chai
2个回答
24
投票

使用普通 Chai(无需额外插件),您可以使用

match
:

expect(thatSelectedItem).to.match(/(?:jpg|png|gif)/)

6
投票
expect(thatSelectedItem).to.contain.oneOf(['jpg', 'png', 'gif'])

此版本与 v4.3.0docs)一起发布。

oneOf
可以与
contain
contains
include
includes
链接,这适用于数组和字符串。强烈建议使用它,因为使用它得到的错误消息更加清晰。

© www.soinside.com 2019 - 2024. All rights reserved.