Skip to content

[FactoryBot] Wrong interpretation of attribute as example declaration #2093

@Mth0158

Description

@Mth0158

Expected behavior

When declaring my factories using FactoryBot gem, I want to be able to use an attribute called example without Rubocop complaining about expectations.

Actual behavior

FactoryBot.define do
  factory :word_function_meaning, class: "Word::Function::Meaning" do
    ...
    trait :with_admin_update_attributes do
      definition { Faker::Lorem.sentence(word_count: rand(4..8)) }

      example { definition }
    end
    ...
  end
end
Offenses:

spec/factories/word/function/meanings.rb:15:7: C: RSpec/NoExpectationExample: No expectation found in this example.
      example { definition }
      ^^^^^^^^^^^^^^^^^^^^^^

Here, I need to add # rubocop:disable RSpec/NoExpectationExample around the example { definition } line because I am getting a RSpec/NoExpectationExample: No expectation found in this example..

Steps to reproduce the problem

See above.

RuboCop RSpec version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rake, and others)
output by rubocop -V, include them as well. Here's an example:

$ [bundle exec] rubocop -V
1.78.0 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.45.1, analyzing as Ruby 3.4, running on ruby 3.4.3) [arm64-darwin24]
  - rubocop-factory_bot 2.27.1
  - rubocop-performance 1.25.0
  - rubocop-rails 2.32.0
  - rubocop-rspec 3.6.0
  - rubocop-rspec_rails 2.31.0
  - rubocop-slim 0.3.0

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