I suspect what is happening is that FactoryGirl is resetting that n
on each invocation from autospec
, but the database hasn't been emptied.
First, to check this diagnosis, change your factory to the following:
Factory.define :email_address do |e|
e.sequence(:address) { |n| puts "Email ##{n}"; "factory_#{n}@example.com" }
e.validated true
end
If my diagnosis is correct, there are two possible fixes:
- change FactoryGirl to start from an index greater than the maximum ID used. This would involve some serious hacking on
Factory::Sequence
- you would probably have to turn Factory::Sequence.sequences
from a Hash[Symbol => Proc]
to a Hash[Symbol => [Proc, Integer]
that remembered the highest index it used. Indeed, that might not even work since autospec does seem to be unloading the FactoryGirl classes properly (otherwise the sequence lookup wouldn't be failing and creating a new Factory::Sequence
object).
- figure out why your database isn't being cleared between each autospec run. Have you checked your teardown methods? Does your testing database support transactions?