2010-11-12 15:58:25 -05:00
|
|
|
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
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-07-30 09:49:12 -04:00
|
|
|
factory :user do
|
|
|
|
factory :user_with_name do
|
2018-07-29 11:30:02 -04:00
|
|
|
name { "John Doe" }
|
2011-07-30 09:49:12 -04:00
|
|
|
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
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.user_id).to eq 1
|
2011-07-30 09:49:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "assigning an association by passing factory" do
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.create(:post_with_named_user).user }
|
2011-07-30 09:49:12 -04:00
|
|
|
|
|
|
|
it "assigns attributes correctly" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.name).to eq "John Doe"
|
|
|
|
expect(subject.age).to eq 20
|
2011-07-30 09:49:12 -04:00
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|