You can define a controller method on the fly in order to test this
out. I just did a quick experiment to demonstrate it…obviously
modify to suit your needs.
You can define a controller method on the fly in order to test this
out. I just did a quick experiment to demonstrate it…obviously
modify to suit your needs.
Thanks Pat.
I think I’m confusing two issues.
How to test before filters for something like authentication
How to test functions provided in the application controller
I’m not putting the before filter in the application.rb, I’m likely to
be using it to protect various actions in the app.
Is there a way to to call methods directly within the application.rb,
spec them there and then sub for functionality in the other controllers?
Andy
PS Your ‘controller spec’ blog post was the ‘light-going-on-in-my-head’
moment for RSpec. Thanks for that!
You can still use the technique that I showed, you would just call
before_filter in the fake controller. That would allow you to specify
and implement the filter in isolation.
Aha! Success, although I needed to add in a little Route fixing to make
it work.
application_spec.rb:
describe ApplicationController, “storing locations” do
class FooController < ApplicationController
before_filter :assign_var
def index; render :text => “foos”; end
end
controller_name :foo
before(:each) do
ActionController::Routing::Routes.draw do |map|
map.resources :foo
end
end
it “should assign the current user” do
get :index
assigns[:var].should_not be_blank
end
end
application.rb:
class ApplicationController < ActionController::Base
def assign_var @var = “Quantum Leap Rocks”
end
end
How to test functions provided in the application controller
I’m not putting the before filter in the application.rb, I’m likely to
be using it to protect various actions in the app.
You can still use the technique that I showed, you would just call
before_filter in the fake controller. That would allow you to specify
and implement the filter in isolation.
Is there a way to to call methods directly within the application.rb,
spec them there and then sub for functionality in the other
controllers?
No to the first part, unfortunately. Rails’ controller design isn’t
particularly test-friendly.
As far as using them in other controllers, you can just use the real
filter implementation if you want, or stub them if you prefer.