# How to write test about validates\_associated

**URL:** <https://www.ruby-forum.com/t/how-to-write-test-about-validates-associated/141679>\
**Category:** Rails\
**Created:** [June 29, 2008, 5:16am UTC](https://www.ruby-forum.com/t/how-to-write-test-about-validates-associated/141679 "2008-06-29T05:16:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [June 29, 2008, 5:16am UTC](https://www.ruby-forum.com/t/how-to-write-test-about-validates-associated/141679/1 "2008-06-29T05:16:14Z")

</div>

Here is my model:

class Account \< ActiveRecord::Base

has\_many :users, :dependent =\> :destroy  
has\_many :bus  
belongs\_to :currency  
has\_many :mylogs, :dependent =\> :destroy  
has\_many :batchuserdefines, :dependent =\> :destroy  
has\_many :materialuserdefines, :dependent =\> :destroy  
has\_many :materials  
has\_many :inventories  
has\_many :batchs  
has\_many :courses  
has\_many :projects  
has\_many :initstocks,:dependent =\> :destroy  
has\_many :vouchertypes, :dependent =\> :destroy  
has\_one :coursecodeformat, :dependent =\> :destroy

validates\_uniqueness\_of :name\_native  
validates\_uniqueness\_of :code  
validates\_presence\_of :code  
validates\_presence\_of :name\_native  
validates\_presence\_of :email  
validates\_presence\_of :phone  
validates\_associated :currency  
validates\_associated :coursecodeformat  
validates\_associated :vouchertypes  
end

and here is my test:

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class AccountTest \< Test::Unit::TestCase

def test\_validate\_present  
account = Account.new  
assert !account.valid?  
assert account.errors.invalid?(:name\_native)  
assert !account.errors.invalid?(:code)  
assert account.errors.invalid?(:email)  
assert account.errors.invalid?(:phone)  
assert account.errors.invalid?(:currency)  
assert account.errors.invalid?(:coursecodeformat)  
assert account.errors.invalid?(:vouchertypes)  
end  
end

and the resualt:  
test\_validate\_present(AccountTest) [test/unit/account\_test.rb:12]:  
(false) is not true.

It looks the test failed at assert account.errors.invalid?(:currency)

I feel puzzle. The model and the test looks simple,why failed

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [June 29, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/how-to-write-test-about-validates-associated/141679/2 "2008-06-29T17:27:59Z")

</div>

Michael G. wrote:

> Here is my model:
> 
> class Account \< ActiveRecord::Base  
> belongs\_to :currency  
> validates\_associated :currency  
> validates\_associated :coursecodeformat  
> validates\_associated :vouchertypes  
> end
> 
> and here is my test:
> 
> require File.dirname( **FILE** ) + ‘/…/test\_helper’
> 
> class AccountTest \< Test::Unit::TestCase
> 
> def test\_validate\_present  
> account = Account.new  
> assert account.errors.invalid?(:currency)  
> end  
> end
> 
> and the resualt:  
> test\_validate\_present(AccountTest) [test/unit/account\_test.rb:12]:  
> (false) is not true.
> 
> It looks the test failed at assert account.errors.invalid?(:currency)
> 
> I feel puzzle. The model and the test looks simple,why failed

I think you mis-understood the docs

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001055](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001055)

validates\_associated - “Validates whether the associated object or  
objects are all valid themselves”

ie. it doesn’t validate that there is a currency,  
it just says something like;

def account.errors.invalid?(:currency)  
if account.currency  
account.currency.invalid?  
else  
return false  
end  
end

so at the moment the test would only pass if you did

account = Account.new  
account.build\_currency(:something =\> :invalid)  
…

think maybe “validates\_presence\_of :currency” will work for you.

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [June 29, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/how-to-write-test-about-validates-associated/141679/3 "2008-06-29T22:00:50Z")

</div>

Ok,it work,thanks
