NielsH
NielsH

Reputation: 697

rspec ignores skip_before_filter?

We are having a strange problem. In the application controller we have a before_filter set that requires authentication using devise and redirects if needed to the login page.

In our library controller we skip this before_filter.

skip_before_filter :authenticate_user!, :only => :show

When we run a simple functional test with capybara and rspec the test fails.

it "should get only english articles within the category 'computers'" do
    visit '/en/library/computers'
    page.should have_content('computers')
end

It looks like it doesn't skip this filter. The page content is of the login page.

When we run this with rails server it works fine.

Any ideas why it behaves this way or what to look for to resolve this?

UPDATE:

It might be worth adding that this only happens using Linux. Under MacOS 10.7 with the "same" setup it works fine.

Controller Code:

class Library::CategoriesController < ApplicationController
  skip_before_filter :authenticate_user!, :only => [:show]

  # GET /categories/1
  # GET /categories/1.json
  def show

    @categories = Category.all
    @category = Category.find(params[:id])

    @articles = @category.articles.where(:locale => I18n.locale)
    respond_to do |format|
      format.html # show.html.erb
    end
  end
end

The application_controller looks like (without the set_i18n_locale_from_url):

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_i18n_locale_from_url, :authenticate_user!
end

Routes:

namespace :library do
  get '/' => 'library#index', :as => 'library'
  resources :categories, :path => '', :only => [:show]
  resources :categories, :path => '', :only => []  do
    resources :articles, :path => '', :only => [:show]
  end
end

Upvotes: 5

Views: 1573

Answers (2)

NielsH
NielsH

Reputation: 697

I think it has something to do with the factories and the different localizations we have for a category model. Probably under linux it uses a different default locale and that's why it fails.

I removed the before_filter for the authentication and most tests pass, but this occurs:

 1) Categories when user requests the category page 
      GET /en/library/computers 
        should get only english articles within the category computers
    Failure/Error: page.should have_content(@english_articles[0].title)
      expected there to be content "article 1 en" in "Bibliothek\nAdmin\nSign up\nLogin\n\n\n\n\n\n\n\n\nHome\n>\n\n\nLibrary\n>\n\n\Computer\n>\n\nThe spine\n\n\n\n\n\n\n\n\n\nComputer\n\n\n\n\n\n\n\n\n\n\n\n\nComputer\n\n\n\narticle 6 de\n\ncontent_6\n\n\narticle 7 de\n\ncontent_7\n\n\narticle 8 de\n\ncontent_8\n\n\narticle 9 de\n\ncontent_9\n\n\narticle 10 de\n\ncontent_10"
    # ./spec/requests/categories_spec.rb:20:in `block (4 levels) in <top (required)>'

Upvotes: 1

Chris Barretto
Chris Barretto

Reputation: 9529

I guess the first thing you can do is to see whether or not "visit '/en/library/computers'" is actually calling the show action in your controller. If you are using a restful path that accesses the show action, use that in place of your current url path so it looks something like:

visit library_path('computers')

Next throw some debugger text (puts 'blah') inside of your show method that you are testing against and make sure it is coming up. Maybe by calling a strict path does something weird with bypassing before_filters.

UPDATE:

Looks like your controller might be no be interpreting 'computers' as an id. Inside your routes, what if you add a member route to your libraries resource:

resources :libraries do
  member do
    get :computers
  end
end

And in your controller your add:

def computers
  @categories = Category.all
end

And change you skip_before_filter to use :computers

Upvotes: 3

Related Questions