Skip to content

Instantly share code, notes, and snippets.

@erikdstock
Created April 20, 2017 14:34
Show Gist options
  • Save erikdstock/686c039943ca274619c59c238943147d to your computer and use it in GitHub Desktop.
Save erikdstock/686c039943ca274619c59c238943147d to your computer and use it in GitHub Desktop.
Query param matchers for rspec/rails
# Match query params on some url somewhere in a text [actually a mail.body, hence the to_s]
# Not tested in production
RSpec::Matchers.define :include_query_params do |expected|
match do |actual|
query_finder = /http\S+\?(\S+)["\s'>]/
queries = query_finder.match actual.to_s
queries.captures.any? do |q|
q_hash = Rack::Utils.parse_nested_query(q).symbolize_keys
q_hash.merge(expected) == q_hash
end
end
end
# Match a single query parameter somewhere in the a url [very dumb]
RSpec::Matchers.define :query_param do |expected|
expected = expected.to_query
match do |actual|
actual.include?(expected)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment