require 'spec_helper' describe ActiveRemote::Association do let(:record) { double(:record) } let(:records) { [ record ] } describe '.belongs_to' do context 'simple association' do let(:author_guid) { 'AUT-123' } subject { Post.new(:author_guid => author_guid) } it { should respond_to(:author) } it 'searches the associated model for a single record' do Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records) subject.author.should eq record end it 'memoizes the result record' do Author.should_receive(:search).once.with(:guid => subject.author_guid).and_return(records) 3.times { subject.author.should eq record } end context 'when the search is empty' do it 'returns a nil' do Author.should_receive(:search).with(:guid => subject.author_guid).and_return([]) subject.author.should be_nil end end end context 'specific association with class name' do let(:author_guid) { 'AUT-456' } subject { Post.new(:author_guid => author_guid) } it { should respond_to(:coauthor) } it 'searches the associated model for a single record' do Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records) subject.author.should eq record end end context 'specific association with class name and foreign_key' do let(:author_guid) { 'AUT-456' } subject { Post.new(:author_guid => author_guid) } it { should respond_to(:bestseller) } it 'searches the associated model for a single record' do Author.should_receive(:search).with(:guid => subject.bestseller_guid).and_return(records) subject.author.should eq record end end end describe '.has_many' do let(:records) { [ record, record, record ] } let(:guid) { 'AUT-123' } subject { Author.new(:guid => guid) } it { should respond_to(:posts) } it 'searches the associated model for all associated records' do Post.should_receive(:search).with(:author_guid => subject.guid).and_return(records) subject.posts.should eq records end it 'memoizes the result record' do Post.should_receive(:search).once.with(:author_guid => subject.guid).and_return(records) 3.times { subject.posts.should eq records } end context 'when the search is empty' do it 'returns the empty set' do Post.should_receive(:search).with(:author_guid => subject.guid).and_return([]) subject.posts.should be_empty end end context 'specific association with class name' do it { should respond_to(:flagged_posts) } it 'searches the associated model for a single record' do Post.should_receive(:search).with(:author_guid => subject.guid).and_return([]) subject.flagged_posts.should be_empty end end context 'specific association with class name and foreign_key' do it { should respond_to(:bestseller_posts) } it 'searches the associated model for multiple record' do Post.should_receive(:search).with(:bestseller_guid => subject.guid).and_return(records) subject.bestseller_posts.should eq(records) end end end describe '.has_one' do let(:guid) { 'PST-123' } subject { Post.new(:guid => guid) } it { should respond_to(:category) } it 'searches the associated model for all associated records' do Category.should_receive(:search).with(:post_guid => subject.guid).and_return(records) subject.category.should eq record end it 'memoizes the result record' do Category.should_receive(:search).once.with(:post_guid => subject.guid).and_return(records) 3.times { subject.category.should eq record } end context 'when the search is empty' do it 'returns a nil value' do Category.should_receive(:search).with(:post_guid => subject.guid).and_return([]) subject.category.should be_nil end end context 'specific association with class name' do it { should respond_to(:main_category) } it 'searches the associated model for a single record' do Category.should_receive(:search).with(:post_guid => subject.guid).and_return(records) subject.main_category.should eq record end end context 'specific association with class name and foreign_key' do it { should respond_to(:default_category) } it 'searches the associated model for a single record' do Category.should_receive(:search).with(:template_post_guid => subject.guid).and_return(records) subject.default_category.should eq record end end end end