什么是"模糊的正则表达文字"在rubocop?

时间:2016-07-12 15:53:02

标签: rubocop

以下代码

expect(foo).to match /#{MyGem.config.environment_name}/

触发rubocop问题

Warning: Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the / if it should be a division.

有人可以解释问题是什么以及如何解决它?

2 个答案:

答案 0 :(得分:8)

解决这个问题的另一种方法是像rubocop建议的那样简单地添加parens。变化

expect(foo).to match /#{MyGem.config.environment_name}/

expect(foo).to match(/#{MyGem.config.environment_name}/)

答案 1 :(得分:2)

此时抱怨说:

match /#{MyGem.config.environment_name}/

目前还不清楚您是否将两个数字分开或将RegExp文字传递给方法调用。

在这种特殊情况下,由于您只是检查字符串中是否存在子字符串,因此最好使用RSpec #include匹配器,如下所示:

expect(foo).to include MyGem.config.environment_name