2010-11-12 15:58:25 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "attribute aliases" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('User', name: :string, age: :integer)
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Post', user_id: :integer) do
|
2010-11-12 15:58:25 -05:00
|
|
|
belongs_to :user
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
2011-07-30 09:49:12 -04:00
|
|
|
factory :user do
|
|
|
|
factory :user_with_name do
|
|
|
|
name "John Doe"
|
|
|
|
end
|
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
|
|
|
|
factory :post do
|
|
|
|
user
|
|
|
|
end
|
2011-07-30 09:49:12 -04:00
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
factory :post_with_named_user, class: Post do
|
|
|
|
user factory: :user_with_name, age: 20
|
2011-07-30 09:49:12 -04:00
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-30 09:49:12 -04:00
|
|
|
context "assigning an association by foreign key" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl.build(:post, user_id: 1) }
|
2011-07-30 09:49:12 -04:00
|
|
|
|
|
|
|
it "doesn't assign both an association and its foreign key" do
|
|
|
|
subject.user_id.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "assigning an association by passing factory" do
|
|
|
|
subject { FactoryGirl.create(:post_with_named_user).user }
|
|
|
|
|
|
|
|
it "assigns attributes correctly" do
|
|
|
|
subject.name.should == "John Doe"
|
|
|
|
subject.age.should == 20
|
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|