[rspec-users] disabling before filters in rspec?
dchelimsky at gmail.com
Mon Jan 4 05:20:39 EST 2010
On Sun, Jan 3, 2010 at 6:56 PM, jollyroger <timo.roessner at googlemail.com>wrote:
> Hey guys,
> i have this before-filter in my application-controller:
> def login_required
> if !current_user
> redirect_to join_welcome_path and return
> This before-filter is prepended before all other before-filters in my
> application_controller.rb like this
> prepend_before_filter :login_required,
> Now i want to test a controller which should only be accessible if
> the user is logged in like that:
> describe "new action" do
> describe "before-filter requirements are not met" do
> before(:each) do
> it "should redirect to join_welcome_path if not logged in" do
> get :new
> response.should redirect_to(join_welcome_path)
> The thing is, I would have expected this spec to pass - a call to
> "new" invokes the login-required before-filter which redirects to the
> Instead i get:
> NoMethodError in 'Shopping::PaymentsController new action before-
> filter requirements are not met should redirect to join_welcome_path
> if not logged in'
> undefined method `current_cart_size' for nil:NilClass
> which is from a before-filter which is triggered _after_ the login-
> required before-filter.
> How do I even get there?
> As far as I understood the docs, this line:
> should cause a redirect,
That is not correct. That line completely replaces the login_required()
method with a stub that returns false. The code in the real login_required()
method is not executed at all, and therefore the redirect does not happen.
> so how do i even get to subsequent before-
Filters only halt execution if they invoke a redirect or return. In this
case, because the filter is stubbed and the redirect is never called,
In a nutshell:
> Why do I even reach subsequent before-filters?
> What am I doing wrong here?
You have a couple of options here, but I think the simplest thing would be
to stub current_user instead of login_required:
it "should redirect to join_welcome_path if not logged in" do
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the rspec-users