Qwertie
Qwertie

Reputation: 6573

FactoryBot uninitialized constant FactoryName

I have the factory

FactoryBot.define do
    factory :activity_fit_file do
        association :user, factory: :user
        activity_type {:cycling}
        after(:build) do |activity|
            activity.original_activity_log_file.attach(
                io: File.open("#{Rails.root}/spec/files/example_fit_file.fit"),
                filename: 'example_fit_file.fit',
                content_type: 'application/vnd.ant.fit'
            )
        end
    end
end

and in my spec I have

require 'rails_helper'

RSpec.describe "Activity upload fixer" do
    it 'converts fit files to gpx' do
        activity = FactoryBot.create(:activity_fit_file)
    end
end

Running the spec gives the error

 NameError:
   uninitialized constant ActivityFitFile

Not quite sure what is wrong because it seems the same as other working factories.

Upvotes: 5

Views: 3450

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230561

You can have custom factory names, but you have to specify the actual classes then.

factory :activity_fit_file, class: 'Activity' do
  ···
end

Upvotes: 15

Related Questions