The following code
expect(foo).to match /#{MyGem.config.environment_name}/
triggers a rubocop issue
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.
Can someone explain what the issue is and how to resolve it?
Another way to fix this is to simply add parens as rubocop suggests. Change
expect(foo).to match /#{MyGem.config.environment_name}/
to
expect(foo).to match(/#{MyGem.config.environment_name}/)
It is complaining that at this point:
match /#{MyGem.config.environment_name}/
it is unclear whether you're dividing two numbers or passing a RegExp literal to a method call.
In this particulare case, since you're just checking for the presence of a substring within a string, it would be better to use the RSpec #include matcher, like so:
expect(foo).to include MyGem.config.environment_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With