2015-08-31 2 views
2

, пожалуйста, помогите тестировать ассоциацию. У меня есть модели:как тестирование ассоциации в рельсах?

class Album < ActiveRecord::Base 
    belongs_to :user 
end 

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

я Протестируйте ассоциации:

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

после испытания выполнения в консоли, я получаю последующие:

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:in block (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

ответ

1

Вы можете проверить эту связь легко с помощью belong_to Искателя из драгоценный камень shoulda:

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

См. this for an example.

Смежные вопросы