Skip to content

Commit

Permalink
Add acceptance spec covering assigning associations via :factory
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jul 30, 2011
1 parent d4edf6f commit 841e012
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions spec/acceptance/attribute_aliases_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,44 @@

describe "attribute aliases" do
before do
define_model('User')
define_model('User', :name => :string, :age => :integer)

define_model('Post', :user_id => :integer) do
belongs_to :user
end

FactoryGirl.define do
factory :user
factory :user do
factory :user_with_name do
name "John Doe"
end
end

factory :post do
user
end

factory :post_with_named_user, :class => Post do
user :factory => :user_with_name, :age => 20
end
end
end

it "doesn't assign both an association and its foreign key" do
FactoryGirl.build(:post, :user_id => 1).user_id.should == 1
context "assigning an association by foreign key" do
subject { FactoryGirl.build(:post, :user_id => 1) }

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
end
end

0 comments on commit 841e012

Please sign in to comment.