FactoryBot uninitialized constant FactoryName
Asked Answered
F

1

5

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.

Frescobaldi answered 12/3, 2019 at 12:6 Comment(2)
Do you have a model called ActivityFitFile?Broadtail
@SergioTulentsev No. I don't think there should be. The model is Activity and this is just an activity factory with different data. Typing that out though made me realize there is no way for factorybot to know what the actual model is called so I must have done something wrong thereFrescobaldi
B
15

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

factory :activity_fit_file, class: 'Activity' do
  ···
end
Broadtail answered 12/3, 2019 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.