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

Already on GitHub? Sign in to your account

Spring compatibility #120

Closed
jonleighton opened this Issue Jan 3, 2014 · 3 comments

Comments

Projects
None yet
4 participants

Hello,

Rails 4.1 applications will be generated with Spring installed by default. Quite a few spring users have reported issues with using spring with factory girl. For example:

factory_girl_rails loads its factory definitions in an after_initialize block in the Railtie. This means that it happens on application initialisation, which is fine if you are initialising the application every time you run the tests, but problematic otherwise (i.e. with spring). Symptoms of this problem are:

  • Changes to factories are not reflected when running tests with spring
  • ActiveRecord::AssociationTypeMismatch errors due to factory girl holding onto references of constants which then subsequently get reloaded by Spring and ActiveSupport::Dependencies

There is a simple fix for users:

Spring.after_fork { FactoryGirl.reload }

However it would probably save a lot of confusion if a fix for this issue was baked into factory_girl_rails.

A quick and dirty way of doing this would be to add to the after_initialize:

Spring.after_fork { FactoryGirl.reload } if defined?(Spring)

A cleaner fix would perhaps be to lazy-load the definitions somehow.

I'm not a user of factory girl so not particularly motivated to work on this, but I thought I'd write down what I know in case it is useful, as I looked into the nature of the problem in order to decide if it can be mitigated on spring's side (I don't think it can).

Here's an example app which shows the problems: https://github.com/jonleighton/factory_girl_spring_problem

Owner

joshuaclayton commented Jan 3, 2014

Thanks for this @jonleighton! Internally, we've been using:

RSpec.configure do |config|
  config.before(:suite) do
    FactoryGirl.reload
  end
end

Obviously, this doesn't work with other frameworks, so it's great to see this hook available. We'll get this added shortly.

joshuaclayton added a commit that referenced this issue Jan 3, 2014

joshuaclayton added a commit that referenced this issue Jan 13, 2014

@joshuaclayton Thanks ❤️ That worked for me 😄

joshuaclayton added a commit that referenced this issue Feb 12, 2014

👍 Thanks guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment