Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created January 10, 2011 13:14
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 dchelimsky/772758 to your computer and use it in GitHub Desktop.
Save dchelimsky/772758 to your computer and use it in GitHub Desktop.
Potential approach for rspec-2 matchers using Capybara::string
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
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)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment