Factory Sequence of girls under motorsport

I have this Factory:

Factory.define :email_address do |e| e.sequence(:address) { |n| "factory_#{n}@example.com" } e.validated true end 

When I run my specs with rake spec , it works fine.

When I run autospec, it doesn't work right away, claiming that the email address is used twice in two different objects (there is a check that limits this).

Why does it work differently under autospec?

+4
source share
2 answers

Sometimes, when you interrupt a test suite with Ctrl + C, it can lead to database pollution. Since your database is dirty, creating new objects will have validation conflicts. Just run rake db:test:clone again and everything will be fine.

+3
source

I suspect that FactoryGirl is dropping this n on every call from autospec , but the database has not been emptied.

First, to verify this diagnosis, change the 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, two possible errors are possible:

  • modify FactoryGirl to start with an index greater than the maximum identifier used. This will require a serious hack of Factory::Sequence - you will probably have to turn Factory::Sequence.sequences from Hash[Symbol => Proc] into Hash[Symbol => [Proc, Integer] , which remembers the highest index that it used . Indeed, this may not even work, since autospec seems to correctly unload FactoryGirl classes (otherwise the sequence search will fail and create a new Factory::Sequence object).
  • find out why your database is not cleared between each run of autospec. Have you tested your tracking methods? Does your test database support transactions?
+1
source

Source: https://habr.com/ru/post/1312382/


All Articles