Correct way of testing “associations” with Rspec?

前端 未结 4 1589
情深已故
情深已故 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:22

    RSpec is a ruby test framework, and not a rails framework. belongs_to is a rails construct, and not a ruby construct. Gems like shoulda-matchers connect ruby and rails things and help you write good tests.

    Having the above in mind and following official documentation, should help you stay up to date and understand what you are writing.

    So, below is what I would write.

    User model:

    RSpec.describe User, type: :model do
      context 'associations' do
        it { should have_many(:teams).class_name('Team') }
      end
    end
    

    Team model:

    RSpec.describe Team, type: :model do
      context 'associations' do
        it { should belong_to(:user).class_name('User') }
      end
    end
    

提交回复
热议问题