Do you have your :count sequence defined in factories/factories.rb? I have a
similar case defined in this file and it works fine:
I have everything above defined in one file: test/factories.rb. Is
that a problem?
The doc (Usage · thoughtbot/factory_bot Wiki · GitHub) says
it’s okay to put your factory-related code here…
Why are you using the same sequence in two different factories?
I just want a way to number things differently for multiple
factories. I originally had a sequence called :article_title, but
then I didn’t want to make something SO similar later
with :page_title, and I all I really need is number that increments…
OK. Got it. Now what’s the question?
I get an error in the factory for :page, but not an error
for :article, regarding the sequence :count.
This suggests that I can’t call “Factory.next(:count)” in different
factories.
Is this the case? Or is there something wrong with my methodology?
If you have a sequence called :xyz and you are calling it in a
factory, YOU NEED TO HAVE BRACKETS around the Factory.next(:xyz)
call. In my case:
Factories
Factory.define :article do |t|
t.association :section # ‘belongs to a section’
t.title {|x| “Article#{Factory.next(:count)}” }
(…)
end
(…)
Factory.define :page do |t|
t.title { “Page#{Factory.next(:count)}” } # I originally
didn’t have brackets here; that’s why it didn’t work, while it did
work above!
(…)
end
Sequences
Factory.sequence :count do |n|
n
end
Hope this helps people who come across the same issue.
So would this be a scenario in which it’s better to have a separate
sequence for “something that is more complex” - so like a sequence
called :article_title?
As a note, in the case where you just want a number in a string I
prefer this syntax:
Factory.define :article do |t|
t.association :section
t.sequence(:title) {|n| “Article#{n}”}
end
That way you don’t have a sequence who’s only job is to replicate this
functionality; save those sequences for something that is more complex
that you want to reuse and count.
So would this be a scenario in which it’s better to have a separate
sequence for “something that is more complex” - so like a sequence
called :article_title?
The tricky part there is that you want both the sequence and the
handle to the object that you’re creating. I think that means you
can’t use the shortcut syntax I had (or at least I don’t see how), but
it also means you can’t use a Factory.sequence :foobar do … end,
because that won’t know your x object and it doesn’t look like you can
pass in a context like you can with the other Factory objects.
It seems like you have three options which are:
Do something like what you’re doing, although if you need to share
part of it you could make a sequence :article_title for
“article_#{n}” and then do
You could not put the x.section.name in the title; that’d be my
first inclination as it’d seem odd if the name you have is required to
be that, since its just for testing purposes.
You could patch factory_girl’s sequence to take a context like the
object factories. Look at https://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/sequence.rb;
basically you want to override the next method to take options and
pass them into the call in there. I’m not sure exactly how the object
part does it, but in theory one should be able to look at that and
emulate the appropriate stuff.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.