Search code examples
ruby-on-railsruby-on-rails-4rspecfactory-botrspec-rails

FactoryGirl - create_list increments parameter for each object


I'm trying to use RSpec's create_list in a way that, for a specific column, each object receives the value of the previous object + 1.

If Rails accepted ++ notation, it would be something like this:

create_list :entity, 10, priority: priority++

I tried using sequence, but the problem is the sequence never resets. I need priority to always start from 1.

How could I achieve that?

Thanks in advance.


Solution

  • I do not think this is possible with create_list. Due to the way method calling works in Ruby, the arguments you pass to create_list are evaluated immediately and are not re-evaluated for each new Entity that is being created.

    How about this instead?

    (1..10).map do |priority|
      create :entity, priority: priority
    end