Checking ActiveRecord Associations in RSpec

前端 未结 4 1383
长情又很酷
长情又很酷 2021-02-05 10:16

I am learning how to write test cases using Rspec. I have a simple Post Comments Scaffold where a Post can have many Comments. I am testing this using Rspec. How should i go abo

4条回答
  •  离开以前
    2021-02-05 10:34

    Since ActiveRecord associations should be well-tested by the Rails test suite (and they are), most people don't feel the need to make sure they work -- it's just assumed that they will.

    If you want to make sure that your model is using those associations, that's something different, and you're not wrong for wanting to test that. I like to do this using the shoulda gem. It lets you do neat things like this:

    describe Post do
      it { should have_many(:comments).dependent(:destroy) }
    end
    

提交回复
热议问题