require 'test_helper' class AssertionsTest < Hermes::IntegrationCase fixtures :users context "#assert_current_path" do setup do visit '/users' end test 'assert equal path' do assert_current_path '/users' end test 'refuse different path' do msg = %Q(<"/not_a_valid_path"> expected but was\n<"/users">.) assert_failure(msg) do assert_current_path '/not_a_valid_path' end end end context "#assert_difference_on_click_button" do setup do visit '/users/new' end test 'assert difference exists on click button' do fill_in :name, :with => 'User' assert_difference_on_click_button('Submit', 'User.count') end test "refuse if difference doesn't exist" do # There's already a user in fixtures msg = "\"User.count\" didn't change by 1.\n" << "<2> expected but was\n" << "<1>." assert_failure msg do assert_difference_on_click_button('Submit', 'User.count') end end end context "#assert_difference_on_click_link" do test 'assert difference exists on click link' do create_user visit '/users' assert_difference_on_click_link 'Block', 'User.blocked.count' end test "refuse if difference doesn't exist" do msg = "\"User.blocked.count\" didn't change by 1.\n" << "<1> expected but was\n" << "<0>." create_user visit '/users' assert_failure msg do assert_difference_on_click_link 'New', 'User.blocked.count' end end end context "#assert_content" do test 'assert content on success' do visit '/users' assert_content 'New' end test 'refuse content that does not exist' do random_content = random_string visit '/users' assert_failure(/Expected to have #{random_content}/)do assert_content random_content end end end context "#assert_no_content" do test 'assert no content on success' do visit '/users' assert_no_content random_string end test 'refuse content that does not exist' do visit '/users' assert_failure(/Expected to have no content New/)do assert_no_content 'New' end end end context "#assert_xpath" do test 'assert_xpath on success' do visit '/users' assert_xpath "//div[@id='users']" end test 'refuse content that does not exist' do random_content = random_string visit '/users' assert_failure(/Expected to have xpath/) do assert_xpath "//div[@id='#{random_content}']" end end end context "#assert_no_xpath" do test 'assert_no_xpath on success' do random_content = random_string visit '/users' assert_no_xpath "//div[@id='#{random_content}']" end test 'refuse content that does not exist' do visit '/users' assert_failure(/Expected to have no xpath/) do assert_no_xpath "//div[@id='users']" end end end context "#assert_css" do test 'assert_css on success' do visit '/users' assert_css "div#users" end test 'refuse content that does not exist' do random_content = random_string visit '/users' assert_failure(/Expected to have css/) do assert_css "div##{random_content}" end end end context "#assert_no_css" do test 'assert_no_css on success' do random_content = random_string visit '/users' assert_no_css "div##{random_content}" end test 'refuse content that does not exist' do visit '/users' assert_failure(/Expected to have no css/) do assert_no_css "div#users" end end end context "#assert_link" do test 'assert_link on success' do visit '/users' assert_link "New" end test 'refuse content that does not exist' do random_content = random_string visit '/users' assert_failure(/Expected to have link/) do assert_link random_content end end end context "#assert_no_link" do test 'assert_no_link on success' do random_content = random_string visit '/users' assert_no_link random_content end test 'refuse content that does not exist' do visit '/users' assert_failure(/Expected to have no link/) do assert_no_link "New" end end end context "#assert_select" do test 'assert_select on success' do visit '/users/new' assert_select "Useless select" end test 'refuse content that does not exist' do random_content = random_string visit '/users/new' assert_failure(/Expected to have select/) do assert_select random_content end end end context "#assert_no_select" do test 'assert_no_select on success' do random_content = random_string visit '/users/new' assert_no_select random_content end test 'refuse select that does exist' do visit '/users/new' assert_failure(/Expected to have no select/) do assert_no_select "Useless select" end end end context "#assert_field" do test 'assert_field on success' do visit '/users/new' assert_field "Name" end test 'refuse field that does not exist' do random_content = random_string visit '/users/new' assert_failure(/Expected to have field/) do assert_field random_content end end end context "#assert_no_field" do test 'assert_no_field on success' do random_content = random_string visit '/users/new' assert_no_field random_content end test 'refuse field that does exist' do visit '/users/new' assert_failure(/Expected to have no field/) do assert_no_field "Name" end end end context "#assert_button" do test 'assert_button on success' do visit '/users/new' assert_button "Submit" end test 'refuse button that does not exist' do random_content = random_string visit '/users/new' assert_failure(/Expected to have button/) do assert_button random_content end end end context "#assert_no_button" do test 'assert_no_button on success' do random_content = random_string visit '/users/new' assert_no_button random_content end test 'refuse button that does exist' do visit '/users/new' assert_failure(/Expected to have no button/) do assert_no_button 'Submit' end end end context "#assert_checked_field" do test 'assert_checked_field on success' do visit '/users/new' assert_checked_field "Useless checkbox" end test 'refuse checkbox that does not exist' do random_content = random_string visit '/users/new' assert_failure(/Expected to have checked field/) do assert_checked_field random_content end end end context "#assert_unchecked_field" do test 'assert_unchecked_field on success' do visit '/users/new' assert_unchecked_field "Useless unchecked checkbox" end test 'refuse unchecked field that does not exist' do random_content = random_string visit '/users/new' assert_failure(/Expected to have unchecked field/) do assert_unchecked_field random_content end end end context "#assert_selector" do test 'assert_selector on success' do visit '/users' assert_selector :xpath, "//div[@id='users']" end test 'refuse selector that does not exist' do random_content = random_string visit '/users' assert_failure(/Expected to have selector/) do assert_selector :xpath, "//div[@id='#{random_content}']" end end end context "#assert_no_selector" do test 'assert_no_selector on success' do random_content = random_string visit '/users' assert_no_selector "div##{random_content}" end test 'refuse selector that does exist' do visit '/users' assert_failure(/Expected to have no selector/) do assert_no_selector "div#users" end end end context "#assert_table" do test 'assert_table on success' do visit '/users/new' assert_table "Useless Table", :rows => table_rows end test 'refuse table that does not exist' do random_content = random_string visit '/users/new' assert_failure(/Expected to have table/) do assert_table random_content end end end context "#assert_no_table" do test 'assert_no_table on success' do random_content = random_string visit '/users/new' assert_no_table random_content end test 'refuse table that does exist' do visit '/users/new' assert_failure(/Expected to have no table/) do assert_no_table "Useless Table", :rows => table_rows end end end context "#assert_mail_deliveries" do teardown do ActionMailer::Base.deliveries = [] end test 'verify deliveries size with 1 new email' do assert_mail_deliveries do ActionMailer::Base.deliveries << '123' end end test 'refuse verification with different deliveries' do msg = %Q("ActionMailer::Base.deliveries.size" didn't change by 1.\n) << "<1> expected but was\n<2>." assert_failure(msg) do assert_mail_deliveries do 2.times { ActionMailer::Base.deliveries << '123' } end end end test 'verify deliveries with custom size' do assert_mail_deliveries(3) do 3.times { ActionMailer::Base.deliveries << '123' } end end test 'refuse verification with custom size' do msg = %Q("ActionMailer::Base.deliveries.size" didn't change by 2.\n) << "<2> expected but was\n<3>." assert_failure(msg) do assert_mail_deliveries(2) do 3.times { ActionMailer::Base.deliveries << '123' } end end end end context "#assert_link_to" do setup do Rails.application.routes.default_url_options[:host] = 'www.example.com' visit '/users' end test 'assert when link exists, with path' do assert_link_to new_user_path end test 'assert when link exists, with url' do assert_link_to new_user_url end test 'assert when link does not exists' do assert_failure(/Expected to have link to \//) do assert_link_to '/' end end end private def assert_failure(msg, &block) assert_raise_with_message Test::Unit::AssertionFailedError, msg do block.call end end def random_string(length=20) Array.new(length) { rand(36).to_s(36) }.join end def table_rows [ ['Cuba Pete', 'King of the Ramba beat'], ['Lou Bega', 'Mambo No. 5'] ] end end