Problem with Before Filters

Hello,
I am writing controller specs.I want to know how can i write specs which
invlove before filters in controllers.

On Thu, Dec 10, 2009 at 2:32 AM, Amit K. [email protected]
wrote:

Hello,
I am writing controller specs.I want to know how can i write specs which
invlove before filters in controllers.

Generally, before filters are part of the internal implementation and
don’t warrant specification that knows about their existence. For
example, consider a controller action that should only be invoked by
an admin. The spec might look like this:

describe WidgetController do
describe “POST create” do
context “with an anonymous visitor” do
it “redirects to login” do
post :create, :widget => valid_widget_attributes
response.should redirect_to(login_path)
end
end
context “with an ‘admin’” do
it “redirects to the widget list” do
login_as :admin
post :create, :widget => valid_widget_attributes
response.should redirect_to(widgets_path)
end
end
end
end

Typically this authorization would be implemented in a before filter,
but, as you can see, there is no mention of before filters in the
spec.

HTH,
David

Thanks David,
Now in spec when i write login_as :admin then there must be some method
written for login_as?
Now if there are before filters in controller then do we need to write
each and every method or it is indeed taken care of but the developer
and we need to just have to pass the value for that particular before
filters.
For e.g.
If i have a before filter in controller as
before_filter :admin_access_required, :except=>[ :view,
:update_presentation]
Now in my spec do i need to pass only the value like
admin_access_required :admin
or i need to write some method for the same

You can write a stub method that would return a value such that your
object
meets the admin_required_access requirement.

controller.stub(:admin_access_required).and_return(true)

On Mon, Dec 14, 2009 at 3:26 AM, Andrei E. [email protected] wrote:

If i have a before filter in controller as
before_filter :admin_access_required, :except=>[ :view,
:update_presentation]
Now in my spec do i need to pass only the value like
admin_access_required :admin
or i need to write some method for the same

You can write a stub method that would return a value such that your object
meets the admin_required_access requirement.

controller.stub(:admin_access_required).and_return(true)

That could work, but methods like admin_access_required are often
implemented such that they modify the internal state of the
controller, setting the user on its internal session or request
object. If the behaviour subsequently invoked by the example needs to
access that state, this wouldn’t work.

Cheers,
David

On Mon, Dec 14, 2009 at 1:05 AM, Amit K. [email protected]
wrote:

Thanks David,
Now in spec when i write login_as :admin then there must be some method
written for login_as?

That would be a helper that you write yourself or is provided by the
authentication framework you’re using. It lives in the spec suite, not
in the controller code, and does whatever is necessary to either
actually log in. The most black box approach is to post to the
controller that manages logging in.

What it looks like depends on how you design your authentication
system. restful_authentication, for example, ships with login_as and
authorize_as methods that set session variables directly.

HTH,
David

Thanks a lot David.
I am now getting familiar with before filters.
I am writing spec without using mocks so i need to just pass the proper
parameter to get pass the filter.