Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec, test presence of an association

I'm trying to test an association.

I have these two classes:

class Survey < ActiveRecord::Base
  has_many :users, through: :users_surveys
  has_many :users_surveys

  def update_or_create_result(user_id, result, target)
    user_survey = users_surveys.find_or_create_by(user_id: params[:user_id])
    if survey_passed?(result, target)
      user_survey.completed!
    end
  end
end

class User < ActiveRecord::Base
  has_many :surveys, through: :users_surveys
  has_many :users_surveys
end

class UsersSurvey < ActiveRecord::Base
  belongs_to :user
  belongs_to :survey
end

How can I test with RSpec if the association between survey and user is created?

like image 223
Roberto Pezzali Avatar asked Jan 25 '16 10:01

Roberto Pezzali


2 Answers

Just want to emphasize, that current nex syntax for RSpec is the following:

describe User do
  it { is_expected.to have_many(:surveys) } # shortcut for expect(subject).to
end

describe Survey do
  it { is_expected.to belong_to(:user) }
end

should is considered to be old syntax for quite a few years now :)

like image 74
Andrey Deineko Avatar answered Oct 10 '22 17:10

Andrey Deineko


The shoulda-matchers gem provides some nifty matchers that test the association is declared and that the correct database fields and foreign keys exist:

describe User do
  it { should have_many(:surveys) }
end

If you for some reason have gem-o-phobia and just want to test it straight off the bat you can use ActiveRecord::Reflection to get information about the relations of a model:

describe User do
  it "should have_many :surveys" do
    expect(User.reflect_on_association(:surveys).macro).to eq :has_many
  end
end

describe Survey do
  it "should belong_to user" do
    expect(Survey.reflect_on_association(:user).macro).to eq :belongs_to
    expect(Survey.column_names).to include :user_id
  end
end

You can also test the behavior of your models by creating an associated object - however if you have validation requirements than your specs sometimes end up somewhat muddled since your spec for A needs to fullfil the requirements for B.

like image 31
max Avatar answered Oct 10 '22 18:10

max