1

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
1
  • Does it work when you comment or uncomment that line? Does this code work or not? Commented Aug 18, 2011 at 13:40

1 Answer 1

1

Because group.servers isn't an Array. It behaves a lot like one, but it's actually an instance of ActiveRecord::Association::HasManyAssociation. Its ancestor class (ActiveRecord::Association::AssociationProxy) actually passes even class through to an underlying object, so group.servers.class does give Array, 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? Array is true, and the source of assert_instance_of implies that should be good enough.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.