Skip to content

Sequence trouble #94

@bendelonlee

Description

@bendelonlee

Thanks for creating factory bot cypress!

The Problem

Noticing some unexpected behavior for sequences in a test scenario.

create_list works as expected:

items = factory.create_list(:item, 2)
items[0].name 
# => test name 1
items[1].name 
# => test name 2

This does not:

item1 = factory.create(:item)
item2 = factory.create(:item)
item1.name
# => test name 1
item2.name
# => test name 1

It can lead to a lot of boilerplate test setup needing to be added when dealing with items with associations that have uniqueness validators.

My config

My cypress-on-rails version is 1.11.0 (latest).
My factory-bot version is 6.2.

My pretty standard factory_bot.rb
Array.wrap(command_options).map do |factory_options|
  factory_method = factory_options.shift
  begin
    logger.debug "running #{factory_method}, #{factory_options}"
    CypressOnRails::SmartFactoryWrapper.public_send(factory_method, *factory_options)
  rescue => e
    logger.error "#{e.class}: #{e.message}"
    logger.error e.backtrace.join("\n")
    logger.error e.record.inspect.to_s if e.is_a?(ActiveRecord::RecordInvalid)
    raise e
  end
end
My cypress_helper.rb

begin
  require "database_cleaner-active_record"
rescue LoadError => e
  puts e.message
  begin
    require "database_cleaner"
  rescue LoadError => e
    puts e.message
  end
end

begin
  require "factory_bot_rails"
rescue LoadError => e
  puts e.message
end

require "cypress_on_rails/smart_factory_wrapper"

factory = CypressOnRails::SimpleRailsFactory
factory = FactoryBot if defined?(FactoryBot)

CypressOnRails::SmartFactoryWrapper.configure(
  always_reload: !Rails.configuration.cache_classes,
  factory: factory,
  files: [
    Rails.root.join("test", "factories.rb"),
    Rails.root.join("test", "factories", "**", "*.rb"),
  ]
)
My clean.rb
if defined?(DatabaseCleaner)
  DatabaseCleaner.strategy = :truncation
  DatabaseCleaner.clean

  load Rails.root.join("db", "bootstrap.rb")
else
  logger.warn "add database_cleaner or update cypress/app_commands/clean.rb"
end

Rails.logger.info "APPCLEANED" # used by log_fail.rb

Hack-y solution

I can get desired behavior by adding this to the top of the scenario file:

factory.instance.always_reload = false
factory.instance.factory::Internal.sequences.clear

and this to the bottom:

factory.instance.always_reload = true
factory.instance.factory::Internal.sequences.clear

What hasn't worked

Leaving out factory.instance.factory::Internal.sequences.clear makes the sequence not reset between test runs.
I tried adding

CypressOnRails::SmartFactoryWrapper.instance.factory::Internal.sequences.clear

to clean.rb but it didn't have any effect.

Related issue:

#66 was closed, with a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions