Question

I'm pretty new to rails testing and I tried using shoulda but it breaks whatever model I put it in so I figured I should just test them manually.

So my question to you oh great experts of SO is this:

How can I use test::unit to test that a model is associated with another model?

Was it helpful?

Solution

well, the most obvious solution would be something like this:

user = User.create(name: 'User')
2.times { user.friends.create(name: 'Other User') }

assert_equal(2, user.friends.count, "Unexpected associated records count")

but this is basically more like testing rails itself, you should not bother with that.

I would just check if user.respond_to?(:friends). But, of course, it will fail if someone defines a method with that name

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top