This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Refactored assertions to use their has_no_* counterparts

  • Loading branch information...
vinibaggio committed Mar 4, 2011
1 parent a6bf212 commit 2445f5c31a213a01631310bff44b55e3df8f8dd1
Showing with 34 additions and 64 deletions.
  1. +23 −17 lib/hermes/assertions.rb
  2. +11 −47 test/hermes/assertions_test.rb
View
@@ -1,8 +1,19 @@
# TODO: Using has_xpath and has_no_xpath affects performance in Capybara.
# All assert_no_* should use its equivalent has_no_*
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
@@ -30,29 +41,29 @@ def assert_content(content)
def assert_no_content(content)
boolean = has_content?(content)
if boolean
assert_block("#{body_inner_text}\nDid not expect to have content #{content}"){ false }
assert_block("#{body_inner_text}\nExpected to have no content #{content}"){ false }
else
assert_block("assert_no_content") { true }
end
end
%w(xpath css link select field button checked_field unchecked_field selector table).each do |method|
METHODS.each do |inclusion_method, rejection_method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def assert_#{method}(*args, &block)
match = has_#{method}?(*args, &block)
def assert_#{inclusion_method}(*args, &block)
match = has_#{inclusion_method}?(*args, &block)
if match
assert_block("assert_#{method}") { true }
assert_block("assert_#{inclusion_method}") { true }
else
assert_block("\#{page.body}\nExpected to have #{method} \#{args.inspect}"){ false }
assert_block("\#{page.body}\nExpected to have #{inclusion_method.gsub('_', ' ')} \#{args.inspect}"){ false }
end
end
def assert_no_#{method}(*args, &block)
match = has_#{method}?(*args, &block)
def assert_#{rejection_method}(*args, &block)
match = has_#{rejection_method}?(*args, &block)
if match
assert_block("\#{page.body}\nDid not expect to have #{method} \#{args.inspect}"){ false }
assert_block("assert_#{rejection_method}") { true }
else
assert_block("assert_no_#{method}") { true }
assert_block("\#{page.body}\Expected to have #{rejection_method.gsub('_', ' ')} \#{args.inspect}"){ false }
end
end
RUBY
@@ -74,10 +85,5 @@ def assert_link_to(url, options={})
def body_inner_text
Nokogiri::HTML(page.body).inner_text.gsub(/^\s*$/, "").squeeze("\n")
end
# Provide a shortcut to show body inner text. It helps debugging.
def pbit
puts body_inner_text
end
end
end
@@ -91,7 +91,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse content that does not exist' do
visit '/users'
assert_failure(/Did not expect to have content New/)do
assert_failure(/Expected to have no content New/)do
assert_no_content 'New'
end
end
@@ -126,7 +126,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse content that does not exist' do
visit '/users'
assert_failure(/Did not expect to have xpath/) do
assert_failure(/Expected to have no xpath/) do
assert_no_xpath "//div[@id='users']"
end
end
@@ -161,7 +161,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse content that does not exist' do
visit '/users'
assert_failure(/Did not expect to have css/) do
assert_failure(/Expected to have no css/) do
assert_no_css "div#users"
end
end
@@ -196,7 +196,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse content that does not exist' do
visit '/users'
assert_failure(/Did not expect to have link/) do
assert_failure(/Expected to have no link/) do
assert_no_link "New"
end
end
@@ -231,7 +231,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse select that does exist' do
visit '/users/new'
assert_failure(/Did not expect to have select/) do
assert_failure(/Expected to have no select/) do
assert_no_select "Useless select"
end
end
@@ -266,7 +266,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse field that does exist' do
visit '/users/new'
assert_failure(/Did not expect to have field/) do
assert_failure(/Expected to have no field/) do
assert_no_field "Name"
end
end
@@ -301,7 +301,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse button that does exist' do
visit '/users/new'
assert_failure(/Did not expect to have button/) do
assert_failure(/Expected to have no button/) do
assert_no_button 'Submit'
end
end
@@ -318,30 +318,12 @@ class AssertionsTest < Hermes::IntegrationCase
visit '/users/new'
assert_failure(/Expected to have checked_field/) do
assert_failure(/Expected to have checked field/) do
assert_checked_field random_content
end
end
end
context "#assert_no_checked_field" do
test 'assert_no_checked_field on success' do
random_content = random_string
visit '/users/new'
assert_no_checked_field random_content
end
test 'refuse checkbox that does not exist' do
visit '/users/new'
assert_failure(/Did not expect to have checked_field/) do
assert_no_checked_field "Useless checkbox"
end
end
end
context "#assert_unchecked_field" do
test 'assert_unchecked_field on success' do
visit '/users/new'
@@ -353,30 +335,12 @@ class AssertionsTest < Hermes::IntegrationCase
visit '/users/new'
assert_failure(/Expected to have unchecked_field/) do
assert_failure(/Expected to have unchecked field/) do
assert_unchecked_field random_content
end
end
end
context "#assert_no_unchecked_field" do
test 'assert_no_unchecked_field on success' do
random_content = random_string
visit '/users/new'
assert_no_unchecked_field random_content
end
test 'refuse checked field that does exist' do
visit '/users'
assert_failure(/Did not expect to have unchecked_field/) do
assert_no_unchecked_field "Useless unchecked checkbox"
end
end
end
context "#assert_selector" do
test 'assert_selector on success' do
visit '/users'
@@ -406,7 +370,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse selector that does exist' do
visit '/users'
assert_failure(/Did not expect to have selector/) do
assert_failure(/Expected to have no selector/) do
assert_no_selector "div#users"
end
end
@@ -442,7 +406,7 @@ class AssertionsTest < Hermes::IntegrationCase
test 'refuse table that does exist' do
visit '/users/new'
assert_failure(/Did not expect to have table/) do
assert_failure(/Expected to have no table/) do
assert_no_table "Useless Table", :rows => table_rows
end
end

0 comments on commit 2445f5c

Please sign in to comment.