public
Last active

Potential approach for rspec-2 matchers using Capybara::string

  • Download Gist
capybara_string_matchers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
module CapybaraStringMatchers
extend RSpec::Matchers::DSL
 
matcher :have_selector do |selector|
match do |html|
simplified(html).has_selector?(selector)
end
 
failure_message_for_should do |html|
"expected to find selector #{selector.inspect} in:\n#{html.to_s}"
end
 
match_for_should_not do |_|
raise "`should_not have_selector` is not supported. Use `should have_no_selector` instead"
end
 
def simplified(html)
Capybara.string(html)
end
end
 
matcher :have_no_selector do |selector|
match do |html|
simplified(html).has_no_selector?(selector)
end
 
failure_message_for_should do |html|
"expected not to find selector #{selector.inspect} in:\n#{html.to_s}"
end
 
match_for_should_not do |_|
raise "`should_not have_no_selector` is not supported. Use `should have_selector` instead"
end
 
def simplified(html)
Capybara.string(html)
end
end
end
failure_output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Failures:
 
1) things/edit.html.erb renders the edit thing form
Failure/Error: rendered.should have_selector("form input#thing_names")
expected to find selector "form input#thing_names" in:
<h1>Editing thing</h1>
<form accept-charset="UTF-8" action="/things/1001" class="edit_thing" id="edit_thing_1001" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /></div>
<div class="field">
<label for="thing_name">Name</label><br />
<input id="thing_name" name="thing[name]" size="30" type="text" value="MyString" />
</div>
<div class="actions">
<input id="thing_submit" name="commit" type="submit" value="Update Thing" />
</div>
</form>
<a href="/things/1001">Show</a> |
<a href="/things">Back</a>
# ./spec/views/things/edit.html.erb_spec.rb:31:in `block (2 levels) in <top (required)>'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.