Is Rails.Cache Purged Between Tests

is Rails.cache purged between tests?

Add:

before(:all) do
Rails.cache.clear
end

to have the cache cleared before each spec file is run.

Add:

before(:each) do
Rails.cache.clear
end

to have the cache cleared before each spec.

You can put this inside spec/spec_helper.rb within the RSpec.configure block to have it applied globally (recommended over scattering it per spec file or case).

RSpec by default does not clear that cache automatically.

Optionally testing caching in Rails 3 functional tests

You're liable to end up with tests stomping on each other. You should wrap this up with ensure and reset it to old values appropriately. An example:

module ActionController::Testing::Caching
def with_caching(on = true)
caching = ActionController::Base.perform_caching
ActionController::Base.perform_caching = on
yield
ensure
ActionController::Base.perform_caching = caching
end

def without_caching(&block)
with_caching(false, &block)
end
end

I've also put this into a module so you can just include this in your test class or parent class.

Does Rails cache templates?

I had the same question and did some hunting.

Yes - Rails caches templates.

Is it important to deal with query caching in ActiveRecord unit tests?

Yes. Depending on how you write your tests the Rails query cache can sometimes interfere. Sometimes rails is smart enough to keep track of when the cache needs to be cleared (When there is an obvious association between objects), but here is an example that would not behave as expected:

user.posts.should == []
Post.create(:user_id => user.id)
user.posts.size.should_not == [] # Fails, since the original query was cached.

In general, if you are executing the same query twice in the same test you should call .reload on the data prior to attempting to executing the second query. Like so:

user.posts.should == []
Post.create(:user_id => user.id)
user.posts.reload
user.posts.size.should_not == []

In my personal experience it is better to consider a different way of writing the test than to use the method above. For example, here's a better way of writing the above that will not be affected by the query cache:

lambda { Post.create(:user_id => user.id) }.should_change(user.posts, :count).by(1)

Rails/RSpec toggle cache for a single test

in spec_helper.rb

RSpec.configure do |config|
config.before(:example, disable_cache: true) do
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::NullStore.new)
end

config.after(:example, disable_cache: true) do
allow(Rails).to receive(:cache).and_call_original
end
end

in xxx_spec.rb

RSpec.describe "a group without matching metadata" do
it "does not run the hook" do
puts Rails.cache.class
end

it "runs the hook for a single example with matching metadata", disable_cache: true do
puts Rails.cache.class
end
end

https://www.relishapp.com/rspec/rspec-core/docs/hooks/filters

Ruby on Rails: Clear a cached page

This line in development.rb ensures that caching is not happening.

config.action_controller.perform_caching             = false

You can clear the Rails cache with

Rails.cache.clear

That said - I am not convinced this is a caching issue. Are you making changes to the page and not seeing them reflected? You aren't perhaps looking at the live version of that page? I have done that once (blush).

Update:

You can call that command from in the console.
Are you sure you are running the application in development?

The only alternative is that the page that you are trying to render isn't the page that is being rendered.

If you watch the server output you should be able to see the render command when the page is rendered similar to this:

Rendered shared_partials/_latest_featured_video (31.9ms)
Rendered shared_partials/_s_invite_friends (2.9ms)
Rendered layouts/_sidebar (2002.1ms)
Rendered layouts/_footer (2.8ms)
Rendered layouts/_busy_indicator (0.6ms)

Rails autoloading: constants are lost between tests or a circular dependency is introduced

It seems I've found a way to work around this by moving the responsibility of keeping track of registered implementations from the Measure base class to the Measures module, and also doing the registrations in the same file as the module is defined. It seems to me like a more sound architectural choice, too. Only thing left to do is to differentiate the names with more than an 's'-affix.

Rails keeps caching query instead of the result of the query

IIRC ActiveRecord is now "lazy", and you're just caching the query object. Try appending .all after User.paginate() to force the sql to run, giving you a result set to cache.

If .all fails, try .map/.select/.inject instead.



Related Topics



Leave a reply



Submit