Correct way of testing “associations” with Rspec?

前端 未结 4 1554
情深已故
情深已故 2021-01-30 20:49

I am trying to test the following scenario:

-> I have a model called Team which it just makes sense when it has been created by a User. Therefore, each Team instance has

4条回答
  •  悲&欢浪女
    2021-01-30 21:27

    I usually use this approach:

    describe User do
      it "should have many teams" do
        t = User.reflect_on_association(:teams)
        expect(t.macro).to eq(:has_many)
      end
    end
    

    A better solution would be to use the gem shoulda which will allow you to simply:

    describe Team do
      it { should belong_to(:user) }
    end
    

提交回复
热议问题