Test views, be true! :) 
Why?
- you need to test some complex views
- and you want to use RSpec2
- and assert_select seems is something strange to you
- have_tag in rspec-rails are deprecated now
- you need a user-firendly output in your error messages
Being true
Install
Add to your Gemfile (in the :test group :) ):
group :test do gem 'rspec-html-matchers' end
Instructions for installing Nokogiri.
Usage
Simple example:
view=<<-HTML <h1>Simple Form</h1> <p> </p> <p> </p> HTML view.should have_tag('form', :with => { :action => '/users', :method => 'post' }) do with_tag "input", :with => { :name => "user[email]", :type => 'email' } with_tag "input#special_submit", :count => 1 without_tag "h1", :text => 'unneeded tag' without_tag "p", :text => /content/i end
Examples with more description:
- tag matching (matches one or more tags):
'<p class="qwe rty">Paragraph</p>'.should have_tag('p') '<p class="qwe rty">Paragraph</p>'.should have_tag(:p) '<p class="qwe rty">Paragraph</p>'.should have_tag('p#qwerty') '<p class="qwe rty">Paragraph</p>'.should have_tag('p.qwe.rty') '<p class="qwe rty"><strong>Para</strong>graph</p>'.should have_tag('p strong') '<p class="qwe rty"><strong>Para</strong>graph</p>'.should have_tag('p#qwerty strong') '<p class="qwe rty"><strong>Para</strong>graph</p>'.should have_tag('p.qwe.rty strong') # or '<p class="qwe rty"><strong>Para</strong>graph</p>'.should have_tag('p') do with_tag('strong') end '<p class="qwe rty"><strong>Para</strong>graph</p>'.should have_tag('p#qwerty') do with_tag('strong') end '<p class="qwe rty"><strong>Para</strong>graph</p>'.should have_tag('p.qwe.rty') do with_tag('strong') end
- special case: classes matching:
# all of this are equivalent: '<p class="qwe rty">Paragraph</p>'.should have_tag('p', :with => { :class => 'qwe rty' }) '<p class="qwe rty">Paragraph</p>'.should have_tag('p', :with => { :class => 'rty qwe' }) '<p class="qwe rty">Paragraph</p>'.should have_tag('p', :with => { :class => ['rty', 'qwe'] }) '<p class="qwe rty">Paragraph</p>'.should have_tag('p', :with => { :class => ['qwe', 'rty'] })
usage with capybara and cucumber:
page.should have_tag( … )
where page
is an instance of Capybara::Session
Also included special matchers for form inputs:
- have_form
- with_checkbox
- with_email_field
- with_file_field
- with_hidden_field
- with_option
- with_password_field
- with_radio_button
- with_button
- with_select
- with_submit
- with_text_area
- with_text_field
- with_url_field
- with_number_field
- with_range_field
- with_date_field
and of course you can use the without_
matchers (see the documentation).
More info
You can find more on RubyDoc, take a look at {RSpec::Matchers#have_tag have_tag} method.
Also, please read {file:CHANGELOG.md CHANGELOG}, it might be helpful.
Contribution
- Fork the repository
- Add tests for your feature
- Write the code
- Send a pull request
Contributors
- Kelly Felkins
- Ryan Wilcox
- Simon Schoeters
- Felix Tjandrawibawa
- Szymon PrzybyĆ
- [Howard Wilson][https://github.com/watsonbox]
MIT Licensed
Copyright © 2011-2012 Dmitrij Mjakotnyi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.