Checking ActiveRecord Associations in RSpec

前端 未结 4 1379
长情又很酷
长情又很酷 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:49

    If you'd rather not use an external gem like shoulda to test your associations (see Robert Speicher's Answer for details on that), another option is to use reflect_on_association to get the AssociationReflection object for the relevant association, and then assert on that:

    describe Post do
      it "should destroy its comments when it is destroyed" do
        association = Post.reflect_on_association(:comments)
    
        expect(association).to_not be_nil
        expect(association.options[:dependent]).to eq :destroy
      end
    end
    

提交回复
热议问题