Skip to content

Instantly share code, notes, and snippets.

@mirakui
Created August 20, 2012 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirakui/3405104 to your computer and use it in GitHub Desktop.
Save mirakui/3405104 to your computer and use it in GitHub Desktop.
大域的なperform_cachingの値にかかわらずcaches_actionのテストをする
class FooController < ApplicationController
caches_action :index
def index
render :text => Time.now
end
end
require 'spec_helper'
describe FooController do
describe "GET 'index'" do
before(:all) do
request.host = 'example.com'
end
around do |example|
ActionController::Base.cache_store.clear
old_perform_caching = ActionController::Base.perform_caching
ActionController::Base.perform_caching = perform_caching
reload_controller_class FooController
old_controller = @controller
@controller = FooController.new
example.run
@controller = old_controller
ActionController::Base.perform_caching = old_perform_caching
reload_controller_class FooController
ActionController::Base.cache_store.clear
end
describe "caching" do
let(:perform_caching) { true }
before do
get 'index'
end
subject { controller.fragment_exist?('example.com/foo') }
it { should be_true }
end
describe "without caching" do
let(:perform_caching) { false }
before do
get 'index'
end
subject { controller.fragment_exist?('example.com/foo') }
it { should be_false }
end
end
end
# :
# (略)
# :
def reload_controller_class(controller_class)
Object.send(:remove_const, controller_class.to_s)
load Rails.root.join("app/controllers/#{controller_class.to_s.underscore}.rb")
end
@mirakui
Copy link
Author

mirakui commented Aug 20, 2012

$ rspec  -d spec/controllers/foo_controller_spec.rb
..

Finished in 0.09546 seconds
2 examples, 0 failures

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