Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:00:18+00:00 2026-06-06T05:00:18+00:00

Declaring a class for (fast) testing purposes is great: require ‘fast_helper’ require ‘site_search’ class

  • 0

Declaring a class for (fast) testing purposes is great:

require 'fast_helper'
require 'site_search'

class Post; end # This allows not to load the whole Rails env

describe SiteSearch do
  it "searches on posts" do
    Post.stub_chain(:scoped, :by_term).with("ruby").and_return ["post1", "post2"]
    SiteSearch.by_term("ruby").should == ["post1", "post2"]
  end
end

The problem with it is that it seems to break autoloading of rails models when the whole suite of specs is run.

The models aren’t loaded anymore when the class gets declared before.

There are 4 ways of injecting the unloaded dependencies:

  1. Declare the class (as in example here)
  2. Set/remove const
  3. Stub a wrapper method
  4. Actually load them

I only want to use the 1st one.

The question: keeping the same specs structure, how can I tell rails to actually load the models even though the class is already declared?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:00:19+00:00Added an answer on June 6, 2026 at 5:00 am

    In order for your empty class preemption trick to work, you must have set your app config.cache_classes = false, hence eager loading is not happening unless you call

    Rails.application.eager_load!
    

    When running the whole test suite you need to make sure classes preload, then empty redefinition should have no effect.

    Now the question is how you control that it is run only if the full test suite is called. The honest answer is I don’t know, but you can surely control it from the environment. Somewhere in your rspec helpers you initialize rails, update it to something like:

    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    Rails.application.eager_load! unless ENV["FORCE_EAGER_LOAD"].blank?
    

    and then call rspec on full suite as:

    FORCE_EAGER_LOAD=t rspec
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm declaring an instance of a class like so: Matrix m; This appears to
I'm having trouble declaring a const field in an abstract class. Why is this?
I want to call a partial method from outside of the declaring class. This
I'm declaring a class like this: #include BindableInterface.h #include ../LuaHelper.h #include <map> #include <string>
Ok, so lately I got this presumable bad habit of declaring my class properties
Is there a way to work around this?Im declaring class objects in a switch
I find myself declaring bunch of class variables and it is really tiring to
In PHP, What is the difference between declaring methods inside class like public function
I'm declaring an NSString property in a class and objective-c is complaining that: NSString
If one can prevent subclassing by declaring private constructor in the base class, why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.