Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset 'sequence' for FactoryGirl tests

Does anyone know how to reset the sequence method for FactoryGirl?

I have a factory that creates a list of tasks and I want to order to start at 1 every time. I use 'sequence' because the task list is a associated model, so I would need the order to increase every time I use FactoryGirl.create until I call a reset.

like image 817
trev9065 Avatar asked Jul 28 '12 00:07

trev9065


1 Answers

You need to write FactoryGirl.reload in the before/after callback of the test file.

Example code snippet

  before do
    FactoryGirl.reload
    .
    .
    .
  end

The '.' represents other code.

Preferably write FactoryGirl.reload unless FactoryGirl.factories.blank? so that FactoryGirl does not reload, when it does not have to.


Doing a full reload of FactoryGirl might have some overhead on the time it takes to process the test(s); this is also what is described as Anti-Patterns.

like image 98
Crimbo Avatar answered Sep 25 '22 19:09

Crimbo