Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke factory with class name instead of factory name #740

Closed
thermaldonkey opened this issue Jan 30, 2015 · 3 comments
Closed

Invoke factory with class name instead of factory name #740

thermaldonkey opened this issue Jan 30, 2015 · 3 comments

Comments

@thermaldonkey
Copy link

I've got an ActiveRecord model and a basic factory set up for it, under the normal Rails naming convention:

# app/models/panel.rb
class Panel < ActiveRecord::Base
  # model stuff
end

# spec/factories/panels.rb
FactoryGirl.define do
  factory :panel do
    # factory stuff
  end
end

I've noticed in my RSpec tests I can invoke this factory with either the factory name or the model's class name:

FactoryGirl.create(:panel)

FactoryGirl.create(Panel)

The latter is preferable, because it means I can replace literal symbols with RSpec's described_class helper.

So now, I've got another model that I'm attempting to do this for, but when invoking the factory, I see:

ArgumentError:
  Factory not registered: ModelNamespace::OtherModel

I've defined the model and factory as follows:

# app/models/model_namespace/other_model.rb
class ModelNamespace::OtherModel < ActiveRecord::Base
  # database table for model is named 'model_namespace_other_models', yay for inheriting legacy systems!
  # model stuff
end

# spec/factories/model_namespace_other_models.rb
FactoryGirl.define do
  factory(:other_model, class: ModelNamespace::OtherModel) do
    # factory stuff
  end
end

Is there a way I could redefine either to fix the resolution issue, or am I misusing this feature of FactoryGirl?

@ajm188
Copy link
Contributor

ajm188 commented Apr 8, 2015

What if you tried this?

FactoryGirl.define do
  factory(:"ModelNamespace::OtherModel") do
    # factory stuff
  end
end

FactoryGirl.create(ModelNamespace::OtherModel)

@tmichel
Copy link

tmichel commented Apr 10, 2015

I hit this bug as well. For a quick workaround you can register the same (named) factory again with the class name and avoid duplication of all attributes:

FactoryGirl.define do
  factory :panel do
    # factory stuff

    # register the same factory for the class as well
    factory(Panel)
  end
end

monkbroc added a commit to monkbroc/factory_girl that referenced this issue Dec 7, 2015
Namespaced models like `Invoice::Item` should be able to be found by `FactoryGirl.create(Invoice::Item)` if registered as `factory :invoice_item`.

I added a test for this new behavior.

Fixes thoughtbot#740
@joshuaclayton
Copy link
Contributor

This was an unintended effect and has been deprecated: #843

In FG5, only accessing via symbol will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants