Basic Assertions
assert(boolean, message)
assert(person.name == “Johnâ€, “Name was expected to be John.â€)
assert(item.errors.invalid?(:price))
assert_equal(expected, actual, message)
assert_equal(person.name, “Johnâ€, “Name was expected to be John.â€)
assert_equal(“can’t be emptyâ€, product.errors.on(:price))
assert_not_equal(expected, actual, message)
assert_not_equal(person.name, “Maryâ€, “Name was Mary and it should not be.â€)
assert_not_equal(“is not a numberâ€, product.errors.on(:price))
assert_raise(Exception, message) { block… }
assert_raise(ZeroDivisionError, “Cannot divide by zero!â€) { 100 / 0 }
assert_raise(ActiveRecord::RecordNotFound) { Product.find(bad_id) }
assert_nothing_raised(Exception, message) { block… }
assert_nothing_raised(ZeroDivisionError) { 100 / [0,1].max }
assert_nothing_raised(ActiveRecord::RecordNotFound) { Product.find(good_id) }
assert_nil(object, message)
assert_nil( product, “Expected product to be nil.†)
assert_nil( Wine.find(:first, :conditions => ‘id = 1000’) )
assert_not_nil(object, message)
assert_not_nil( product, “Product should not be nil.†)
assert_not_nil( Wine.find(:first, :conditions => ‘id = 1’) )
assert_valid(activerecord_object)
same as: assert(object.valid?)
assert_valid(@person)
assert_valid( Wine.find(1) )
flunk(message)
always fails immediately; same as: assert(false, message)
flunk(“Quantity should not be greater than 100â€) if quantity > 100
flunk(“Either user or account should be validâ€) unless user.valid? || account.valid?
Advanced Assertions
assert_match(pattern, string, message)
assert_match(/^\d,\d{3},\d{3}$/, “1,000,000â€, “Should match this format.â€)
assert_no_match(pattern, string, message)
assert_no_match(/\d{3},\d{2}$/, “1,000,000â€, “Should not match this format.â€)
assert_in_delta(expected_float, actual_float, delta, message)
assert_in_delta(100.0, price, 20.0, “Price should be between 80.00 and 120.00â€)
assert_in_delta(2, length, 1, “Length should be 1-3 feet.â€)
assert_instance_of( klass, object, message )
assert_instance_of( User, person, “person should be an instance of User†)
assert_kind_of( klass, object, message )
assert_kind_of( User, person, “person should be a kind of User†)
assert_kind_of( Class, User, “User should be a kind of Class†)
assert_respond_to( object, symbol, message )
Instances only respond to instance methods, classes only respond to class methods
assert_respond_to( person, :full_name, “No response to full_name†)
assert_respond_to( User, :custom_find, “No response to custom_find†)
assert_throws(expected_symbol, message) { block… }
assert_throws(:done, “Array should be emptyâ€) { throw :done if [].empty? }
assert_nothing_thrown(message) { block… }
assert_nothing_thrown(“Array should not be emptyâ€) { throw :done if [1].empty? }
Rare Assertions & DEFAULT EROR MESSA GES
assert_same( expected, actual, message)
same as: assert_equal(expected, actual)
assert_same( person.name, “Johnâ€)
assert_not_same( expected, actual, message)
same as: assert_not_equal(expected, actual)
assert_not_same( person.name, “Maryâ€)
assert_operator( object1, operator, object2, message )
same as: assert( object1.operator(object2) )
assert_operator( 1000, :<, 2000, “Expected 1000 to be less than 2000†)
assert_operator( user, :old_enough?, Time.now(), “User should be old enoughâ€)
assert_send([receiver, symbol, arg1, arg2], message)
same as: assert( receiver.message(arg1, arg2) )
assert_send([product, :decrement_inventory, qty], “Decrement should succeedâ€)
From: /activerecord/lib/active_record/validations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| @@default_error_messages = {
:inclusion => “is not included in the listâ€,
:exclusion => “is reservedâ€,
:invalid => “is invalidâ€,
:confirmation => “doesn’t match confirmationâ€,
:accepted => “must be acceptedâ€,
:empty => “can’t be emptyâ€,
:blank => “can’t be blankâ€,
:too_long => “is too long (maximum is %d characters)â€,
:too_short => “is too short (minimum is %d characters)â€,
:wrong_length => “is the wrong length (should be %d characters)â€,
:taken => “has already been takenâ€,
:not_a_number => “is not a numberâ€,
:greater_than => “must be greater than %dâ€,
:greater_than_or_equal_to => “must be greater than or equal to %dâ€,
:equal_to => “must be equal to %dâ€,
:less_than => “must be less than %dâ€,
:less_than_or_equal_to => “must be less than or equal to %dâ€,
:odd => “must be oddâ€,
:even => “must be evenâ€
} |
@@default_error_messages = {
:inclusion => “is not included in the listâ€,
:exclusion => “is reservedâ€,
:invalid => “is invalidâ€,
:confirmation => “doesn’t match confirmationâ€,
:accepted => “must be acceptedâ€,
:empty => “can’t be emptyâ€,
:blank => “can’t be blankâ€,
:too_long => “is too long (maximum is %d characters)â€,
:too_short => “is too short (minimum is %d characters)â€,
:wrong_length => “is the wrong length (should be %d characters)â€,
:taken => “has already been takenâ€,
:not_a_number => “is not a numberâ€,
:greater_than => “must be greater than %dâ€,
:greater_than_or_equal_to => “must be greater than or equal to %dâ€,
:equal_to => “must be equal to %dâ€,
:less_than => “must be less than %dâ€,
:less_than_or_equal_to => “must be less than or equal to %dâ€,
:odd => “must be oddâ€,
:even => “must be evenâ€
}
Referência:
http://www.nullislove.com/2008/02/20/testing-in-rails-part-10-assertions/