mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
bf04aaa068
Most of this was fixed by adding the `attribute-defined-statically-cop` branch of `thoughtbot/rubocop-rspec` to the Gemfile and running: ```sh rubocop \ --require rubocop-rspec \ --only FactoryBot/AttributeDefinedStatically \ --auto-correct ``` I had to update the cucumber tests manually, and I realized our changes don't handle `ignore` blocks or blocks with arity 1 that use the yielded DefinitionProxy. I will update https://github.com/rubocop-hq/rubocop-rspec/pull/666to handle these cases.
42 lines
974 B
Ruby
42 lines
974 B
Ruby
describe "attribute aliases" do
|
|
before do
|
|
define_model('User', name: :string, age: :integer)
|
|
|
|
define_model('Post', user_id: :integer) do
|
|
belongs_to :user
|
|
end
|
|
|
|
FactoryBot.define do
|
|
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
|
|
|
|
context "assigning an association by foreign key" do
|
|
subject { FactoryBot.build(:post, user_id: 1) }
|
|
|
|
it "doesn't assign both an association and its foreign key" do
|
|
expect(subject.user_id).to eq 1
|
|
end
|
|
end
|
|
|
|
context "assigning an association by passing factory" do
|
|
subject { FactoryBot.create(:post_with_named_user).user }
|
|
|
|
it "assigns attributes correctly" do
|
|
expect(subject.name).to eq "John Doe"
|
|
expect(subject.age).to eq 20
|
|
end
|
|
end
|
|
end
|