鲁波考的"Ambiguous regexp literal"是什么?



以下代码

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.

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

解决此问题的另一种方法是简单地添加括号,如rubocop建议的那样。 改变

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

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

在这一点上,它抱怨:

match /#{MyGem.config.environment_name}/

目前尚不清楚您是要除以两个数字还是将 RegExp 文本传递给方法调用。

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

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

最新更新