Mark Bolusmjak
Mark Bolusmjak

Reputation: 24399

Why is Rails 4 not loading a class that Rails 3 would load?

In my test_helper.rb file I override one method on one model for testing purposes. Let's call it Foo.

Suppose I add debug statements as such:

#in app/models/foo.rb
class Foo
  puts "loading class Foo from app/models"
  ...
end

and:

#in test/test_hepler.rb
class Foo
  puts "loading class Foo from test/test_hepler"
  ...
end

In in Rails 3, I see on the console:

>loading class Foo from app/models
>loading class Foo from test/test_hepler

In Rails 4, (even with config.eager_load = true) I see only:

>loading class Foo from test/test_hepler

Why this difference? Also, what is the preferred fix? Simply require 'Foo' before I open it for extension?

Upvotes: 2

Views: 64

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

If you change test_helper to do

Foo.class_eval do
  ...
end 

Then rails is forced to load your Foo model (so that it can call class_eval on it)

As to why the test initialisation process changed you would have to dig back through the changelogs/commits - there definitely were changes around how/when you app code is preloaded.

Upvotes: 2

Related Questions