Skip to content

Instantly share code, notes, and snippets.

@elomatreb
Created July 17, 2017 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elomatreb/0f6673f5fca7f98483cac9c5c4a7e9f5 to your computer and use it in GitHub Desktop.
Save elomatreb/0f6673f5fca7f98483cac9c5c4a7e9f5 to your computer and use it in GitHub Desktop.
Duct-tape HTML testing. No capybaras were required and/or harmed during development
require "nokogiri"
RSpec::Matchers.define :have_elements_matching do |selector|
match do |view|
Nokogiri::HTML(view).match(selector).any?
end
end
RSpec::Matchers.define :have_one_element_matching do |selector|
match do |view|
Nokogiri::HTML(view).match(selector).size == 1
end
end
RSpec::Matchers.define :have_input do |name|
match do |view|
# Workaround for Nokogiri weirdness since it apparently doesn't understand
# escaped CSS selectors
Nokogiri::HTML(view).css("input").any? { |e| e["name"] == name }
end
end
# Type-specific matchers lol
%w[text email password].each do |type|
RSpec::Matchers.define :"have_#{type}_input" do |name = nil|
match do |view|
# See above
if name
Nokogiri::HTML(view).css("input").any? { |e| e["name"] == name && e["type"] == type }
else
Nokogiri::HTML(view).css("input").any? { |e| e["type"] == type }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment