Last active
August 12, 2022 19:42
-
-
Save SirRawlins/8956101 to your computer and use it in GitHub Desktop.
RSpec url matcher.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Drop this into /spec/support/matchers | |
# Usage: result.should be_url | |
# Passes if result is a valid url, returns error "expected result to be url" if not. | |
# Matcher to see if a string is a URL or not. | |
RSpec::Matchers.define :be_url do |expected| | |
# The match method, returns true if valie, false if not. | |
match do |actual| | |
# Use the URI library to parse the string, returning false if this fails. | |
URI.parse(actual) rescue false | |
end | |
end |
Thanks for the matcher.
It would be nice if implemented in rspec project directly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, ESLint was whining about the unused block variable,
expected
andURI:regexp
being obsolet. It preferred the following: