module Hermes # A few assertions built on top of Capybara. module Assertions METHODS = { 'xpath' => 'no_xpath', 'css' => 'no_css', 'link' => 'no_link', 'select' => 'no_select', 'field' => 'no_field', 'button' => 'no_button', 'checked_field' => 'unchecked_field', 'selector' => 'no_selector', 'table' => 'no_table' }.freeze def assert_current_path(expected) assert_equal expected, current_path end def assert_difference_on_click_button(button, *args) assert_difference(*args) do click_button button end end def assert_difference_on_click_link(link, *args) assert_difference(*args) do click_link link end end def assert_content(content) if has_content?(content) assert_block("assert_content") { true } else assert_block("#{body_inner_text}\nExpected to have #{content}"){ false } end end def assert_no_content(content) boolean = has_content?(content) if boolean assert_block("#{body_inner_text}\nExpected to have no content #{content}"){ false } else assert_block("assert_no_content") { true } end end METHODS.each do |inclusion_method, rejection_method| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def assert_#{inclusion_method}(*args, &block) match = has_#{inclusion_method}?(*args, &block) if match assert_block("assert_#{inclusion_method}") { true } else assert_block("\#{page.body}\nExpected to have #{inclusion_method.gsub('_', ' ')} \#{args.inspect}"){ false } end end def assert_#{rejection_method}(*args, &block) match = has_#{rejection_method}?(*args, &block) if match assert_block("assert_#{rejection_method}") { true } else assert_block("\#{page.body}\Expected to have #{rejection_method.gsub('_', ' ')} \#{args.inspect}"){ false } end end RUBY end def assert_mail_deliveries(many=1) assert_difference("ActionMailer::Base.deliveries.size", many){ yield } end def assert_link_to(url, options={}) msg = "#{page.body}\nExpected to have link to #{url}" assert_block(msg) do has_css? "a[href*='%s']" % url, options end end protected def body_inner_text Nokogiri::HTML(page.body).inner_text.gsub(/^\s*$/, "").squeeze("\n") end end end