-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FactoryBot/FactoryAssociationWithStrategy cop error when something is defined in a transient #61
Comments
What does the build(:address) do exactly? |
If the visit factory is created without supplying an address to it, so This is to save passing a visitable object with a known address object attached to the factory each time we want to create a visit object and do stuff with the address in specs. |
Is address a model and an association in visits model? |
No, it is an association on the visit's class Job
has_one :address
has_many :visits, as: :visitable
end FactoryBot.define do
factory :job, class: "Job" do
address factory: %i[address]
end
end Then let's say we had a spec doing something like this: let!(:visit) { create(:visit, address: build(:address, postcode: "W1 1TX")) }
before do
create(:visit, address: build(:address, postcode: "NW1 2ED"))
end
# Return all the visit's who's visitable (job) postcode matches
it { expect(Visit.by_visitable_postcode("W1 1TX")).to eq([visit]) } Setting the address in the let!(:visit) do
job = create(:job, address: build(:address, postcode: "W1 1TX"))
create(:visit, visitable: job)
end
before do
job = create(:job, address: build(:address, postcode: "NW1 2ED"))
create(:visit, visitable: job)
end
it { expect(Visit.by_visitable_postcode("W1 1TX")).to eq([visit]) } |
Not sure if this is related to #54 but the issue I am having is that when something has been defined in a
transient
block that is usingbuild
orcreate
, then rubocop throws aFactoryBot/FactoryAssociationWithStrategy
error thinking it is an association.For example this:
Leads to this:
spec/factories/visits.rb:6:17: C: FactoryBot/FactoryAssociationWithStrategy: Use an implicit, explicit or inline definition instead of hard coding a strategy for setting association within factory. address { build(:address) }
I know in this example I could get around this by changing the above code to
address { nil }
andevaluator.address || build(:address)
but I have some other factories doing the same that aren't so straightforward to amend.Rubocop Gems:
The text was updated successfully, but these errors were encountered: