2

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?

4

1 回答 1

6

好吧,最明显的解决方案是这样的:

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

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

但这基本上更像是测试rails本身,你不应该为此烦恼。

我只是检查一下user.respond_to?(:friends)。但是,当然,如果有人用那个名字定义了一个方法,它就会失败

于 2013-10-10T16:37:50.787 回答