我们遇到了一个奇怪的问题。在应用程序控制器中,我们有一个before_filter
集,需要使用devise
进行身份验证,并在需要时重定向到登录页面。
在我们的库控制器中,我们跳过此before_filter
。
skip_before_filter :authenticate_user!, :only => :show
当我们使用capybara
和rspec
运行简单的功能测试时,测试失败。
it "should get only english articles within the category 'computers'" do
visit '/en/library/computers'
page.should have_content('computers')
end
看起来它没有跳过此过滤器。页面内容属于登录页面。
当我们使用rails server
运行时,它可以正常工作。
为什么它会以这种方式运行或者想要解决此问题的任何想法?
更新
可能值得补充一点,这只发生在Linux上。在具有“相同”设置的MacOS 10.7下,它可以正常工作。
控制器代码:
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
application_controller看起来像(没有set_i18n_locale_from_url):
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_i18n_locale_from_url, :authenticate_user!
end
路线:
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
答案 0 :(得分:3)
我想你要做的第一件事就是看看“访问'/ en / library / computers'”是否实际上是在你的控制器中调用show动作。如果您使用的是访问show动作的restful路径,请使用它来代替当前的url路径,使其看起来像:
visit library_path('computers')
接下来将一些调试文本(将'blah')放入您正在测试的show方法中,并确保它即将出现。也许通过调用严格的路径可以绕过before_filters做一些奇怪的事情。
更新:
看起来您的控制器可能无法将“计算机”解释为ID。在您的路径中,如果您向库资源添加成员路由该怎么办:
resources :libraries do
member do
get :computers
end
end
在您的控制器中添加:
def computers
@categories = Category.all
end
并将skip_before_filter改为使用:计算机
答案 1 :(得分:1)
我认为它与工厂和我们对类别模型的不同本地化有关。可能在linux下它使用不同的默认语言环境,这就是它失败的原因。
我删除了before_filter以进行身份验证,大多数测试都通过了,但是会发生这种情况:
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)>'