Skip to content

Cop idea: redundant FactoryBot enum traits #10

@pirj

Description

@pirj

FactoryBot has an enum traits feature that given a model:

class Task < ActiveRecord::Base
  enum status: {queued: 0, started: 1, finished: 2}
end

makes the following definitions redundant:

FactoryBot.define do
  factory :task do
    trait :queued do
      status { :queued }
    end

It's near impossible to detect using static analysis, however, I've seen this defined as:

status { Model.status[:queued] }

which is equivalent to the above (just translates a symbol to its DB-level numeric representation explicitly rather than relying on ORM to do that). Those cases are detectable.

NOTE: Enum traits appeared in FactoryBot 6.1, so probably we'll need to introduce version configuration a-la TargetFactoryBotVersion (see rubocop-rails for an example).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions