Hi all
I have written the following unit tests:
def test_invalid_characters_in_abbrevation
@country = Country.new(valid_country_attrs)
assert @country.valid?
%w{ a1 11 09 äü ''(a'' ''d&'' }.each do
|abbr|
@country.abbrevation = abbr
assert(!@country.valid?, "''#{@country.abbrevation}''
should not be
a valid abbrevation because of invalid characters")
assert_equal(@country.errors.on(:abbrevation),
Country.error_messages[:not_allowed_chars_in_abbrevation])
end
end
def test_invalid_abbrevation_length
@country = Country.new(valid_country_attrs)
assert @country.valid?
%w{ a b x D G X aAa BBb CCC DFwFe }.each do |abbr|
@country.abbrevation = abbr
assert(!@country.valid?, "''#{@country.abbrevation}''
should not be
a valid abbrevation because of invalid length")
assert_equal(@country.errors.on(:abbrevation),
Country.error_messages[:abbrevation_has_wrong_length])
end
end
My Country model looks like the following:
class Country < ActiveRecord::Base
validates_presence_of :abbrevation
validates_presence_of :name
def self.error_messages
{ :not_allowed_chars_in_abbrevation => ''must contain only A-Z
(uppercase)'',
:abbrevation_has_wrong_length => ''must be exactly 2 chars
long''}
end
def before_validation
abbrevation.upcase! # Make abbrevation upper case!
end
def after_validation
if !@errors.on(:abbrevation)
@errors.add(:abbrevation,
self.class.error_messages[:not_allowed_chars_in_abbrevation]) unless
abbrevation =~ /^[A-Z]+$/
end
if !@errors.on(:abbrevation)
@errors.add(:abbrevation,
self.class.error_messages[:abbrevation_has_wrong_length]) unless
abbrevation.length == 2
end
end
end
Strangely these tests work perfectly when manually testing them via web
browser. But in my unit tests they abort!
chraftbuech:~/Webwork/pgbookings josh$ ruby test/unit/country_test.rb
Loaded suite test/unit/country_test
Started
.FF...
Finished in 0.13866 seconds.
1) Failure:
test_invalid_abbrevation_length(CountryTest)
[test/unit/country_test.rb:73:in `test_invalid_abbrevation_length''
test/unit/country_test.rb:71:in `each''
test/unit/country_test.rb:71:in
`test_invalid_abbrevation_length'']:
''A'' should not be a valid abbrevation because of invalid
length.
<false> is not true.
2) Failure:
test_invalid_characters_in_abbrevation(CountryTest)
[test/unit/country_test.rb:63:in
`test_invalid_characters_in_abbrevation''
test/unit/country_test.rb:61:in `each''
test/unit/country_test.rb:61:in
`test_invalid_characters_in_abbrevation'']:
''A1'' should not be a valid abbrevation because of invalid
characters.
<false> is not true.
6 tests, 32 assertions, 2 failures, 0 errors
I don''t have any clue why! Anyone can point me into the right
direction?
Thanks a lot
Josh
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---