I has one-to many relationship, one group has many servers. To test this relation, i write this test, but it work only when I uncomented line. Why?
test 'Group can include server' do
group = groups(:default)
group.servers << servers(:default)
# Test work, when I uncomment this line:
# assert_instance_of Array, group.servers
group.save
assert_instance_of Server, group.servers.first
end
Because
group.serversisn’t anArray. It behaves a lot like one, but it’s actually an instance of ActiveRecord::Association::HasManyAssociation. Its ancestor class (ActiveRecord::Association::AssociationProxy) actually passes evenclassthrough to an underlying object, sogroup.servers.classdoes giveArray, but it isn’t really one.Edit: Quick note to say that, while I’m pretty sure this is the reason for your failure, it doesn’t make complete sense; in my test apps, the equivalent of
group.servers.instance_of? Arrayistrue, and the source ofassert_instance_ofimplies that should be good enough.