Sergey
Sergey

Reputation: 1001

how testing association in rails?

please help testing association. i have models:

class Album < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :albums, dependent: :destroy
end

i try testing association:

describe Album do
  describe 'associations' do
     it "belongs_to user" do
        should belongs_to(:user)
     end
  end  
end

after run test in console, i get follow:

Failures: 1) Album associations belongs_to user Failure/Error: should belongs_to(:user) NoMethodError: undefined method belongs_to' for #<RSpec::ExampleGroups::Album::Associations:0x0000000762bc98> # ./spec/models/album_spec.rb:36:inblock (3 levels) in '

Finished in 1.41 seconds (files took 2.32 seconds to load) 7 examples, 1 failure Failed examples: rspec ./spec/models/album_spec.rb:35 # Album associations belongs_to user

Upvotes: 2

Views: 287

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34336

You can test this association easily using belong_to matcher of the gem shoulda:

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

See this for an example.

Upvotes: 1

Related Questions