Forum: Ruby on Rails rake test and validates_inclusion_of

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
on 2007-01-17 23:31
(Received via mailing list)

I am just starting off with Rails, so I apologize in advance if there
is a terribly obvious answer to my problem. I've done some searching
and found similar problems to my own, but nothing which yielded a

I have a handful of models that I am unit testing. The tests for each
unit run successfully, but when I do a rake test, many of the tests

The assertions that fail are always ones that are asserting that a
model, which has a reference to another model, is OK. So for example,
one assertion that passes when I do `ruby
test/unit/department_test.rb`, but fails when I do `rake test` is:

department = => "super department",
         :parent_id => departments(:administration_department).id)

When I print out department.errors, I see that parent_id is invalid
because it "is not included in the list".

The validation I have on parent_id is:

validates_inclusion_of :parent_id, :in => Department.find_all.collect {
|e| }

So, maybe the department table wasn't the best example because it's
acts_as_tree, BUT all the failed assertions are the same:
- they're failing on asserting that an object is good
- the object has a reference to another model
- that reference is being validated with validates_inclusion_of
- that reference never passes validation
- they pass when i run the test script individually

So my hypothesis is that the fixture data isn't being loaded in before
the constraints for the reference_id are being defined, but ONLY in the
rake test environment.

Does that make sense? Am I missing something obvious? Any thoughts
would be appreciated.

I'm using Rails 1.1.6 and MySQL (with no foreign key constrains in the

on 2007-01-20 00:52
(Received via mailing list)
Well, I got no responses to this post explaining why this happens, but
I did work around this issue.

For future reference, in case anyone else has this problem, I fixed the
rake test errors by validating the reference ID in the validate
function instead of using validates_inclusion_of.

So, I removed this:

validates_inclusion_of :parent_id, :in => Department.find_all.collect {
|e| }

... and replaced it with this:

def validate
  unless Department.exists?(parent_id)
    errors.add(:parent_id, "is not a valid department")

on 2007-03-15 22:35
(Received via mailing list)
I was having the same problem just the other day, this solution worked
but I ended up using
This topic is locked and can not be replied to.