# Validations only don't work in tests!

**URL:** <https://www.ruby-forum.com/t/validations-only-dont-work-in-tests/89979>\
**Category:** Rails\
**Created:** [March 24, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/validations-only-dont-work-in-tests/89979 "2007-03-24T14:06:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 24, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/validations-only-dont-work-in-tests/89979/1 "2007-03-24T14:06:06Z")

</div>

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.  
 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.  
 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
